View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jtrolog
4    * %%
5    * Copyright (C) 2012 - 2018 WorkLogic Project
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation, either version 2.1 of the
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package jTrolog.terms;
23  
24  /**
25   * Int class represents the integer prolog data type
26   */
27  @SuppressWarnings({ "serial" })
28  public class Int extends jTrolog.terms.Number {
29  
30  	private int value;
31  
32  	public Int() {
33  
34  	}
35  
36  	public Int(String v) throws NumberFormatException {
37  		value = java.lang.Integer.parseInt(v);
38  	}
39  
40  	public Int(int v) {
41  		value = v;
42  	}
43  
44  	/**
45  	 * Returns the value of the Integer as int
46  	 */
47  	public int intValue() {
48  		return value;
49  	}
50  
51  	/**
52  	 * Returns the value of the Integer as float
53  	 */
54  	public float floatValue() {
55  		return (float) value;
56  	}
57  
58  	/**
59  	 * Returns the value of the Integer as double
60  	 */
61  	public double doubleValue() {
62  		return (double) value;
63  	}
64  
65  	/**
66  	 * Returns the value of the Integer as long
67  	 */
68  	public long longValue() {
69  		return value;
70  	}
71  
72  	public String toString() {
73  		return java.lang.Integer.toString(value);
74  	}
75  
76  	public static Number create(String s) throws NumberFormatException {
77  		try {
78  			return new Int(s);
79  		} catch (NumberFormatException e) {
80  			return new Long(s);
81  		}
82  	}
83  
84  	public boolean equals(Object n) {
85  		if (n instanceof Int)
86  			return longValue() == ((Int) n).longValue();
87  		return false;
88  	}
89  }