View Javadoc
1   package de.fhdw.wtf.persistence.meta;
2   
3   import java.math.BigInteger;
4   
5   import de.fhdw.wtf.persistence.exception.PersistenceException;
6   import de.fhdw.wtf.persistence.facade.ObjectFacade;
7   
8   /**
9    * A class to represent any Integer Value in the database. Intger Values are instances of Integer Type.
10   * 
11   */
12  public class IntegerValue extends Object { // NOPMD: Object != java.lang.Object
13  
14  	/**
15  	 * If there are values which are not already fetched the database is asked.
16  	 */
17  	private static ObjectFacade facade;
18  	
19  	/**
20  	 * set the ObjectFacade, because when there are values which are not already fetched the database is asked.
21  	 * 
22  	 * @param objectFacade
23  	 */
24  	public static void setObjectFacade(final ObjectFacade objectFacade) {
25  		facade = objectFacade;
26  	}
27  	
28  	/**
29  	 * The value of the integer.
30  	 */
31  	private BigInteger value;
32  	
33  	/**
34  	 * The id of the integer in the database.
35  	 */
36  	private Long id;
37  	
38  	/**
39  	 * Constructor for a new integer value. This constructor could be used if only the id is knwown. The value will be
40  	 * fetched from the database.
41  	 * 
42  	 * @param id
43  	 *            A valid id of an integer value, which is present in the database.
44  	 */
45  	public IntegerValue(final long id) {
46  		super(IntegerType.getInstance());
47  		this.id = id;
48  	}
49  	
50  	/**
51  	 * Constructor for a new integer value. This constructor could be used if only a value is known. The id in the
52  	 * database will be determined automatically.
53  	 * 
54  	 * @param value
55  	 *            An integer value.
56  	 */
57  	public IntegerValue(final BigInteger value) {
58  		super(IntegerType.getInstance());
59  		this.value = value;
60  	}
61  	
62  	/**
63  	 * Constructor for an integer value if id and value are knwown.
64  	 * 
65  	 * @param id
66  	 *            The id of the integer value in the database.
67  	 * @param value
68  	 *            The value of the integer value.
69  	 */
70  	public IntegerValue(final Long id, final BigInteger value) {
71  		super(IntegerType.getInstance());
72  		this.id = id;
73  		this.value = value;
74  	}
75  	
76  	/**
77  	 * Getter for value. Lazy loads the value from the database if its unset.
78  	 * 
79  	 * @return Provides the value of this Integer Value Object.
80  	 */
81  	public BigInteger getValue() {
82  		if (this.value == null) {
83  			try {
84  				this.value = facade.getIntForId(this.getId());
85  			} catch (final PersistenceException e) {
86  				throw new Error(e);
87  			}
88  		}
89  		return this.value;
90  	}
91  	
92  	/**
93  	 * Getter for Id. Lazy loads the Id from the database if unset.
94  	 */
95  	@Override
96  	public long getId() {
97  		if (this.id == null) {
98  			try {
99  				this.id = facade.getIdForInteger(this.getValue());
100 			} catch (final PersistenceException e) {
101 				throw new Error(e);
102 			}
103 		}
104 		return this.id;
105 	}
106 	
107 	@Override
108 	public boolean isTheSameAs(final java.lang.Object other) {
109 		if (!(other instanceof IntegerValue)) {
110 			return false;
111 		}
112 		
113 		return this.getId() == ((IntegerValue) other).getId();
114 	}
115 	
116 }