001: /*
002:
003: Derby - Class org.apache.derby.iapi.services.io.FormatableIntHolder
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.services.io;
023:
024: import org.apache.derby.iapi.services.io.ArrayInputStream;
025:
026: import org.apache.derby.iapi.services.io.Formatable;
027: import org.apache.derby.iapi.services.io.FormatIdUtil;
028: import org.apache.derby.iapi.services.io.StoredFormatIds;
029:
030: import java.io.ObjectOutput;
031: import java.io.ObjectInput;
032: import java.io.IOException;
033:
034: /**
035: * A formatable holder for an int.
036: */
037: public class FormatableIntHolder implements Formatable {
038: /********************************************************
039: **
040: ** This class implements Formatable. That means that it
041: ** can write itself to and from a formatted stream. If
042: ** you add more fields to this class, make sure that you
043: ** also write/read them with the writeExternal()/readExternal()
044: ** methods.
045: **
046: ** If, inbetween releases, you add more fields to this class,
047: ** then you should bump the version number emitted by the getTypeFormatId()
048: ** method.
049: **
050: ********************************************************/
051:
052: // the int
053: private int theInt;
054:
055: /**
056: * Niladic constructor for formatable
057: */
058: public FormatableIntHolder() {
059: }
060:
061: /**
062: * Construct a FormatableIntHolder using the input int.
063: *
064: * @param theInt the int to hold
065: */
066: public FormatableIntHolder(int theInt) {
067: this .theInt = theInt;
068: }
069:
070: /**
071: * Set the held int to the input int.
072: *
073: * @param theInt the int to hold
074: */
075: public void setInt(int theInt) {
076: this .theInt = theInt;
077: }
078:
079: /**
080: * Get the held int.
081: *
082: * @return The held int.
083: */
084: public int getInt() {
085: return theInt;
086: }
087:
088: /**
089: * Create and return an array of FormatableIntHolders
090: * given an array of ints.
091: *
092: * @param theInts The array of ints
093: *
094: * @return An array of FormatableIntHolders
095: */
096: public static FormatableIntHolder[] getFormatableIntHolders(
097: int[] theInts) {
098: if (theInts == null) {
099: return null;
100: }
101:
102: FormatableIntHolder[] fihArray = new FormatableIntHolder[theInts.length];
103:
104: for (int index = 0; index < theInts.length; index++) {
105: fihArray[index] = new FormatableIntHolder(theInts[index]);
106: }
107: return fihArray;
108: }
109:
110: //////////////////////////////////////////////
111: //
112: // FORMATABLE
113: //
114: //////////////////////////////////////////////
115: /**
116: * Write this formatable out
117: *
118: * @param out write bytes here
119: *
120: * @exception IOException thrown on error
121: */
122: public void writeExternal(ObjectOutput out) throws IOException {
123: out.writeInt(theInt);
124: }
125:
126: /**
127: * Read this formatable from a stream of stored objects.
128: *
129: * @param in read this.
130: *
131: * @exception IOException thrown on error
132: */
133: public void readExternal(ObjectInput in) throws IOException {
134: theInt = in.readInt();
135: }
136:
137: public void readExternal(ArrayInputStream in) throws IOException {
138: theInt = in.readInt();
139: }
140:
141: /**
142: * Get the formatID which corresponds to this class.
143: *
144: * @return the formatID of this class
145: */
146: public int getTypeFormatId() {
147: return StoredFormatIds.FORMATABLE_INT_HOLDER_V01_ID;
148: }
149: }
|