Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / util / MutableInteger.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.util;
19
20 /**
21  * an object that holds a mutable int. Unfortunately, java.lang.Integer is
22  * final, but we can at least be a Number
23  */
24 public class MutableInteger extends Number {
25
26   private int value;
27   
28   public MutableInteger (int val){
29     value = val;
30   }
31   
32   public void set (int val){
33     value = val;
34   }
35   
36   //--- arithmetic operations
37   public MutableInteger inc() {
38     value++;
39     return this;
40   }
41   
42   public MutableInteger dec() {
43     value--;
44     return this;
45   }
46   
47   public MutableInteger add (int n){
48     value += n;
49     return this;
50   }
51   
52   public MutableInteger subtract (int n){
53     value -= n;
54     return this;
55   }
56   
57   public MutableInteger multiply (int n){
58     value *= n;
59     return this;
60   }
61   
62   public MutableInteger divide (int n){
63     value /= n;
64     return this;
65   }
66   
67   //-- Hmm, we probably want to round correctly for these
68   public MutableInteger add (Number n){
69     value += n.intValue();
70     return this;
71   }
72   
73   public MutableInteger subtract (Number n){
74     value -= n.intValue();
75     return this;
76   }
77   
78   public MutableInteger multiply (Number n){
79     value *= n.intValue();
80     return this;
81   }
82   
83   public MutableInteger divide (Number n){
84     value /= n.intValue();
85     return this;
86   }
87   
88   //--- value accessors
89   
90   @Override
91   public double doubleValue() {
92     return value;
93   }
94
95   @Override
96   public float floatValue() {
97     return value;
98   }
99
100   @Override
101   public int intValue() {
102     return value;
103   }
104
105   @Override
106   public long longValue() {
107     return value;
108   }
109 }