1 /* 2 * Copyright 2005 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.google.common.geometry; 17 18 /** 19 * Like an Integer, but mutable :) 20 * 21 * Sometimes it is just really convenient to be able to pass a MutableInteger 22 * as a parameter to a function, or for synchronization purposes (so that you 23 * can guard access to an int value without creating a separate Object just to 24 * synchronize on). 25 * 26 * NOT thread-safe 27 * 28 */ 29 public class MutableInteger { 30 31 private int value; 32 private Integer cachedIntegerValue = null; 33 MutableInteger(final int i)34 public MutableInteger(final int i) { 35 value = i; 36 } 37 intValue()38 public int intValue() { 39 return value; 40 } 41 integerValue()42 public Integer integerValue() { 43 if (cachedIntegerValue == null) { 44 cachedIntegerValue = intValue(); 45 } 46 return cachedIntegerValue; 47 } 48 49 @Override equals(final Object o)50 public boolean equals(final Object o) { 51 return o instanceof MutableInteger && ((MutableInteger) o).value == this.value; 52 } 53 54 @Override hashCode()55 public int hashCode() { 56 return integerValue().hashCode(); 57 } 58 setValue(final int value)59 public void setValue(final int value) { 60 this.value = value; 61 cachedIntegerValue = null; 62 } 63 increment()64 public void increment() { 65 add(1); 66 } 67 add(final int amount)68 public void add(final int amount) { 69 setValue(value + amount); 70 } 71 decrement()72 public void decrement() { 73 subtract(1); 74 } 75 subtract(final int amount)76 public void subtract(final int amount) { 77 add(amount * -1); 78 } 79 80 @Override toString()81 public String toString() { 82 return String.valueOf(value); 83 } 84 } 85