Skip to main content
summaryrefslogtreecommitdiffstats
blob: 538b590acea7a0e9985ba9410d8497f9c0b7b36c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*******************************************************************************
 * Copyright (c) 2005, 2009 committers of openArchitectureWare and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/

package org.eclipse.internal.xtend.type.baseimpl.types;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.eclipse.internal.xtend.type.baseimpl.OperationImpl;
import org.eclipse.xtend.expression.TypeSystem;
import org.eclipse.xtend.typesystem.Feature;
import org.eclipse.xtend.typesystem.Type;

/**
 * @author Sven Efftinge (http://www.efftinge.de)
 * @author Arno Haase
 * @author Heiko Behrens
 */
public final class IntegerTypeImpl extends BuiltinBaseType implements Type {

    public IntegerTypeImpl(final TypeSystem ts, final String name) {
        super(ts, name);
    }

    public boolean isInstance(final Object o) {
        return o instanceof BigInteger || o instanceof Integer || o instanceof Byte || o instanceof Long
                || o instanceof Short;
    }

    public Object newInstance() {
        return new BigInteger("-1");
    }

    @Override
    public Feature[] getContributedFeatures() {
        return new Feature[] {
                new OperationImpl(this, "+", IntegerTypeImpl.this, new Type[] { IntegerTypeImpl.this }) {
                    @Override
                    public Object evaluateInternal(final Object target, final Object[] params) {
                        if (params[0] == null)
                            return null;
                        return toInt(target).add(toInt(params[0]));
                    }
                },
                new OperationImpl(this, "-", IntegerTypeImpl.this, new Type[] { IntegerTypeImpl.this }) {
                    @Override
                    public Object evaluateInternal(final Object target, final Object[] params) {
                        if (params[0] == null)
                            return null;
                        return toInt(target).subtract(toInt(params[0]));
                    }
                },
                new OperationImpl(this, "-", IntegerTypeImpl.this, new Type[] {}) {
                    @Override
                    public Object evaluateInternal(final Object target, final Object[] params) {
                    	return toInt(target).negate();
                    }
                },
                new OperationImpl(this, "*", IntegerTypeImpl.this, new Type[] { IntegerTypeImpl.this }) {
                    @Override
                    public Object evaluateInternal(final Object target, final Object[] params) {
                        if (params[0] == null)
                            return null;

                        return toInt(target).multiply(toInt(params[0]));
                    }
                },
                new OperationImpl(this, "/", IntegerTypeImpl.this, new Type[] { IntegerTypeImpl.this }) {
                    @Override
                    public Object evaluateInternal(final Object target, final Object[] params) {
                        if (params[0] == null)
                            return null;

                        return toInt(target).divide(toInt(params[0]));
                    }
                },
                new OperationImpl(this, "==", getTypeSystem().getBooleanType(), new Type[] { IntegerTypeImpl.this }) {
                    @Override
                    public Object evaluateInternal(final Object target, final Object[] params) {
                        if (target == null)
                            return new Boolean(target == params[0]);
                        try {
                        	return toInt(target).equals(toInt(params[0]));
                        }
                        catch (Exception exc) {
                            return false;
                        }
                    }

                },
                new OperationImpl(this, "!=", getTypeSystem().getBooleanType(), new Type[] { IntegerTypeImpl.this }) {
                    @Override
                    public Object evaluateInternal(final Object target, final Object[] params) {
                        if (target == null)
                            return params[0] != null;

                        try {
                            return ! toInt(target).equals(toInt(params[0]));
                        }
                        catch (Exception exc) {
                            return true;
                        }
                    }
                },
                new OperationImpl(this, ">", getTypeSystem().getBooleanType(), new Type[] { IntegerTypeImpl.this }) {
                    @Override
                    public Object evaluateInternal(final Object target, final Object[] params) {
                        if (target == null)
                            return Boolean.FALSE;
                        if (params[0] == null)
                            return Boolean.FALSE;
                        
                        try {
                        	return toInt(target).compareTo(toInt(params[0])) > 0;
                        }
                        catch (Exception exc) {
                            return Boolean.FALSE; 
                        }
                    }
                },
                new OperationImpl(this, ">=", getTypeSystem().getBooleanType(), new Type[] { IntegerTypeImpl.this }) {
                    @Override
                    public Object evaluateInternal(final Object target, final Object[] params) {
                        if (target == null)
                            return params[0] == null ? Boolean.TRUE : Boolean.FALSE;
                        if (params[0] == null)
                            return Boolean.FALSE;
                        
                        try {
                        	return toInt(target).compareTo(toInt(params[0])) >= 0;
                        }
                        catch (Exception exc) {
                            return Boolean.FALSE; 
                        }
                    }
                },
                new OperationImpl(this, "<", getTypeSystem().getBooleanType(), new Type[] { IntegerTypeImpl.this }) {
                    @Override
                    public Object evaluateInternal(final Object target, final Object[] params) {
                        if (target == null)
                            return Boolean.FALSE;
                        if (params[0] == null)
                            return Boolean.FALSE;

                        try {
                        	return toInt(target).compareTo(toInt(params[0])) < 0;
                        }
                        catch (Exception exc) {
                            return Boolean.FALSE; 
                        }
                    }
                },
                new OperationImpl(this, "<=", getTypeSystem().getBooleanType(), new Type[] { IntegerTypeImpl.this }) {
                    @Override
                    public Object evaluateInternal(final Object target, final Object[] params) {
                        if (target == null)
                            return params[0] == null ? Boolean.TRUE : Boolean.FALSE;
                        if (params[0] == null)
                            return Boolean.FALSE;

                        try {
                        	return toInt(target).compareTo(toInt(params[0])) <= 0;
                        }
                        catch (Exception exc) {
                            return Boolean.FALSE; 
                        }
                    }
                }, new OperationImpl(this, "upTo", getTypeSystem().getListType(this), new Type[] { this }) {

                    @Override
                    public String getDocumentation() {
                        return "returns a List of Integers starting with the value of the target expression, up to "
                                + "the value of the specified Integer, incremented by one.<br/>"
                                + "e.g. '1.upTo(5)' evaluates to {1,2,3,4,5}";
                    }

                    @Override
                    public Object evaluateInternal(final Object target, final Object[] params) {
                        final List<BigInteger> result = new ArrayList<BigInteger>();
                        BigInteger l1 = toInt(target);
                        final BigInteger l2 = toInt(params[0]);

                        while (l1.compareTo(l2) <= 0) {
                            result.add(l1);
                            l1 = l1.add(BigInteger.ONE);
                        }
                        return result;
                    }
                }, new OperationImpl(this, "upTo", getTypeSystem().getListType(this), new Type[] { this, this }) {

                    @Override
                    public String getDocumentation() {
                        return "returns a List of Integers starting with the value of the target expression, up to "
                                + "the value of the first paramter, incremented by the second parameter.<br/>"
                                + "e.g. '1.upTo(10, 2)' evaluates to {1,3,5,7,9}";
                    }

                    @Override
                    public Object evaluateInternal(final Object target, final Object[] params) {
                        final List<BigInteger> result = new ArrayList<BigInteger>();
                        BigInteger l1 = toInt(target);
                        final BigInteger l2 = toInt(params[0]);
                        final BigInteger l3 = toInt(params[1]);

                        while (l1.compareTo(l2) <= 0) {
                            result.add(l1);
                            l1 = l1.add(l3);
                        }
                        return result;
                    }
                } };
    }

	@Override
    public Set<Type> getSuperTypes() {
        return Collections.singleton(getTypeSystem().getRealType());
    }

	protected BigInteger toInt(final Object o) {
		if(o == null)
			return null;
		
		if (o instanceof BigInteger)
			return (BigInteger) o;
		
        if (o instanceof Integer)
            return BigInteger.valueOf(((Integer)o).longValue()); 
        else if (o instanceof Byte)
        	return BigInteger.valueOf(((Byte)o).longValue());
        else if (o instanceof Long)
        	return BigInteger.valueOf((Long)o);
        else if (o instanceof Short)
            return BigInteger.valueOf(((Short) o).longValue());
		
		throw new IllegalArgumentException(o.getClass().getName() + " not supported");
	}
    
    @Override
    public Object convert(final Object src, final Class<?> targetType) {
        final BigInteger value = toInt(src);
        
        if (targetType.isAssignableFrom(BigInteger.class))
        	return value;
        else if (targetType.isAssignableFrom(Long.class) || targetType.isAssignableFrom(Long.TYPE))
        	return value.longValue();
        else if (targetType.isAssignableFrom(Integer.class) || targetType.isAssignableFrom(Integer.TYPE))
            return value.intValue();
        else if (targetType.isAssignableFrom(Byte.class) || targetType.isAssignableFrom(Byte.TYPE))
            return value.byteValue();
        else if (targetType.isAssignableFrom(Short.class) || targetType.isAssignableFrom(Short.TYPE))
            return value.shortValue();
        return super.convert(src, targetType);
    }

}

Back to the top