KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > io > FormatableIntHolder


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.io.FormatableIntHolder
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.io;
23
24 import org.apache.derby.iapi.services.io.ArrayInputStream;
25
26 import org.apache.derby.iapi.services.io.Formatable;
27 import org.apache.derby.iapi.services.io.FormatIdUtil;
28 import org.apache.derby.iapi.services.io.StoredFormatIds;
29
30 import java.io.ObjectOutput JavaDoc;
31 import java.io.ObjectInput JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 /**
35  * A formatable holder for an int.
36  */

37 public class FormatableIntHolder implements Formatable
38 {
39     /********************************************************
40     **
41     ** This class implements Formatable. That means that it
42     ** can write itself to and from a formatted stream. If
43     ** you add more fields to this class, make sure that you
44     ** also write/read them with the writeExternal()/readExternal()
45     ** methods.
46     **
47     ** If, inbetween releases, you add more fields to this class,
48     ** then you should bump the version number emitted by the getTypeFormatId()
49     ** method.
50     **
51     ********************************************************/

52
53     // the int
54
private int theInt;
55     
56     /**
57      * Niladic constructor for formatable
58      */

59     public FormatableIntHolder()
60     {
61     }
62
63     /**
64      * Construct a FormatableIntHolder using the input int.
65      *
66      * @param theInt the int to hold
67      */

68     public FormatableIntHolder(int theInt)
69     {
70         this.theInt = theInt;
71     }
72
73     /**
74      * Set the held int to the input int.
75      *
76      * @param theInt the int to hold
77      */

78     public void setInt(int theInt)
79     {
80         this.theInt = theInt;
81     }
82
83     /**
84      * Get the held int.
85      *
86      * @return The held int.
87      */

88     public int getInt()
89     {
90         return theInt;
91     }
92
93     /**
94      * Create and return an array of FormatableIntHolders
95      * given an array of ints.
96      *
97      * @param theInts The array of ints
98      *
99      * @return An array of FormatableIntHolders
100      */

101     public static FormatableIntHolder[] getFormatableIntHolders(int[] theInts)
102     {
103         if (theInts == null)
104         {
105             return null;
106         }
107
108         FormatableIntHolder[] fihArray = new FormatableIntHolder[theInts.length];
109
110         for (int index = 0; index < theInts.length; index++)
111         {
112             fihArray[index] = new FormatableIntHolder(theInts[index]);
113         }
114         return fihArray;
115     }
116
117     //////////////////////////////////////////////
118
//
119
// FORMATABLE
120
//
121
//////////////////////////////////////////////
122
/**
123      * Write this formatable out
124      *
125      * @param out write bytes here
126      *
127      * @exception IOException thrown on error
128      */

129     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
130     {
131         out.writeInt(theInt);
132     }
133
134     /**
135      * Read this formatable from a stream of stored objects.
136      *
137      * @param in read this.
138      *
139      * @exception IOException thrown on error
140      */

141     public void readExternal(ObjectInput JavaDoc in)
142         throws IOException JavaDoc
143     {
144         theInt = in.readInt();
145     }
146     public void readExternal(ArrayInputStream in)
147         throws IOException JavaDoc
148     {
149         theInt = in.readInt();
150     }
151     
152     /**
153      * Get the formatID which corresponds to this class.
154      *
155      * @return the formatID of this class
156      */

157     public int getTypeFormatId() { return StoredFormatIds.FORMATABLE_INT_HOLDER_V01_ID; }
158 }
159
Popular Tags