Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9c9965680ae736a17e52465b79828759105f5af7 (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
/*******************************************************************************
 * Copyright (c) 2000, 2010 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.compiler.impl;

public class IntConstant extends Constant {

	int value;

	private static final IntConstant MIN_VALUE = new IntConstant(Integer.MIN_VALUE);
	private static final IntConstant MINUS_FOUR = new IntConstant(-4);
	private static final IntConstant MINUS_THREE = new IntConstant(-3);
	private static final IntConstant MINUS_TWO = new IntConstant(-2);
	private static final IntConstant MINUS_ONE = new IntConstant(-1);
	private static final IntConstant ZERO = new IntConstant(0);
	private static final IntConstant ONE = new IntConstant(1);
	private static final IntConstant TWO = new IntConstant(2);
	private static final IntConstant THREE = new IntConstant(3);
	private static final IntConstant FOUR = new IntConstant(4);
	private static final IntConstant FIVE = new IntConstant(5);
	private static final IntConstant SIX = new IntConstant(6);
	private static final IntConstant SEVEN = new IntConstant(7);
	private static final IntConstant EIGHT= new IntConstant(8);
	private static final IntConstant NINE = new IntConstant(9);
	private static final IntConstant TEN = new IntConstant(10);

	public static Constant fromValue(int value) {
		switch (value) {
			case Integer.MIN_VALUE : return IntConstant.MIN_VALUE;
			case -4 : return IntConstant.MINUS_FOUR;
			case -3 : return IntConstant.MINUS_THREE;
			case -2 : return IntConstant.MINUS_TWO;
			case -1 : return IntConstant.MINUS_ONE;
			case 0 : return IntConstant.ZERO;
			case 1 : return IntConstant.ONE;
			case 2 : return IntConstant.TWO;
			case 3 : return IntConstant.THREE;
			case 4 : return IntConstant.FOUR;
			case 5 : return IntConstant.FIVE;
			case 6 : return IntConstant.SIX;
			case 7 : return IntConstant.SEVEN;
			case 8 : return IntConstant.EIGHT;
			case 9 : return IntConstant.NINE;
			case 10 : return IntConstant.TEN;
		}
		return new IntConstant(value);
	}

	private IntConstant(int value) {
		this.value = value;
	}

	@Override
	public byte byteValue() {
		return (byte) this.value;
	}

	@Override
	public char charValue() {
		return (char) this.value;
	}

	@Override
	public double doubleValue() {
		return this.value; // implicit cast to return type
	}

	@Override
	public float floatValue() {
		return this.value; // implicit cast to return type
	}

	@Override
	public int intValue() {
		return this.value;
	}

	@Override
	public long longValue() {
		return this.value; // implicit cast to return type
	}

	@Override
	public short shortValue() {
		return (short) this.value;
	}

	@Override
	public String stringValue() {
		//spec 15.17.11
		return String.valueOf(this.value);
	}

	@Override
	public String toString() {
		return "(int)" + this.value; //$NON-NLS-1$
	}

	@Override
	public int typeID() {
		return T_int;
	}

	@Override
	public int hashCode() {
		return this.value;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		IntConstant other = (IntConstant) obj;
		return this.value == other.value;
	}
}

Back to the top