KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > YInt


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o;
22
23 import com.db4o.foundation.*;
24 import com.db4o.reflect.ReflectClass;
25
26
27 /**
28  * @exclude
29  */

30 public class YInt extends YapJavaClass {
31     
32     private static final Integer JavaDoc i_primitive = new Integer JavaDoc(0);
33     
34     public YInt(YapStream stream) {
35         super(stream);
36     }
37     
38     public Object JavaDoc coerce(ReflectClass claxx, Object JavaDoc obj) {
39         return Coercion4.toInt(obj);
40     }
41
42     public Object JavaDoc defaultValue(){
43         return i_primitive;
44     }
45     
46     public int getID() {
47         return 1;
48     }
49
50     protected Class JavaDoc primitiveJavaClass() {
51         return int.class;
52     }
53
54     public int linkLength() {
55         return YapConst.INT_LENGTH;
56     }
57     
58     public static int max(int x, int y){
59         return (x < y) ? y : x;
60     }
61
62     Object JavaDoc primitiveNull() {
63         return i_primitive;
64     }
65
66     Object JavaDoc read1(YapReader a_bytes) {
67         return new Integer JavaDoc(a_bytes.readInt());
68     }
69
70     static final int readInt(YapReader a_bytes) {
71         if (Deploy.debug) {
72             int ret = 0;
73             a_bytes.readBegin(YapConst.YAPINTEGER);
74             if (Deploy.debugLong) {
75                 ret =
76                     Integer.valueOf(new YapStringIO().read(a_bytes, YapConst.INTEGER_BYTES).trim())
77                         .intValue();
78             } else {
79                 for (int i = 0; i < YapConst.INTEGER_BYTES; i++) {
80                     ret = (ret << 8) + (a_bytes._buffer[a_bytes._offset++] & 0xff);
81                 }
82             }
83             a_bytes.readEnd();
84             return ret;
85         }
86         return a_bytes.readInt();
87     }
88
89     public void write(Object JavaDoc obj, YapReader writer) {
90         write(((Integer JavaDoc) obj).intValue(), writer);
91     }
92
93     public void write(int intValue, YapReader writer) {
94         writeInt(intValue, writer);
95     }
96
97     static final void writeInt(int a_int, YapReader a_bytes) {
98         if (Deploy.debug) {
99             a_bytes.writeBegin(YapConst.YAPINTEGER);
100             if (Deploy.debugLong) {
101                 String JavaDoc l_s = " " + new Integer JavaDoc(a_int).toString();
102                 new YapStringIO().write(
103                     a_bytes,
104                     l_s.substring(l_s.length() - YapConst.INTEGER_BYTES));
105             } else {
106                 for (int i = YapConst.WRITE_LOOP; i >= 0; i -= 8) {
107                     a_bytes._buffer[a_bytes._offset++] = (byte) (a_int >> i);
108                 }
109             }
110             a_bytes.writeEnd();
111         } else {
112             a_bytes.writeInt(a_int);
113         }
114     }
115
116     // Comparison_______________________
117

118     private int i_compareTo;
119
120     protected final int val(Object JavaDoc obj) {
121         return ((Integer JavaDoc) obj).intValue();
122     }
123     
124     public int compareTo(int other){
125         return other - i_compareTo;
126     }
127
128     public void prepareComparison(int i) {
129         i_compareTo = i;
130     }
131     
132     void prepareComparison1(Object JavaDoc obj) {
133         prepareComparison(val(obj));
134     }
135     
136     public Object JavaDoc current1(){
137         return new Integer JavaDoc(currentInt());
138     }
139     
140     public int currentInt(){
141         return i_compareTo;
142     }
143
144     boolean isEqual1(Object JavaDoc obj) {
145         return obj instanceof Integer JavaDoc && val(obj) == i_compareTo;
146     }
147
148     boolean isGreater1(Object JavaDoc obj) {
149         return obj instanceof Integer JavaDoc && val(obj) > i_compareTo;
150     }
151
152     boolean isSmaller1(Object JavaDoc obj) {
153         return obj instanceof Integer JavaDoc && val(obj) < i_compareTo;
154     }
155
156     public void defragIndexEntry(ReaderPair readers) {
157         readers.incrementIntSize();
158     }
159 }
Popular Tags