001: /*
002:
003: Derby - Class org.apache.derby.iapi.services.io.FormatableLongHolder
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.FormatIdUtil;
027: import org.apache.derby.iapi.services.io.Formatable;
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 long.
036: */
037: public class FormatableLongHolder implements Formatable {
038:
039: // the int
040: private long theLong;
041:
042: /**
043: * Niladic constructor for formatable
044: */
045: public FormatableLongHolder() {
046: }
047:
048: /**
049: * Construct a FormatableLongHolder using the input integer.
050: *
051: * @param theLong the long to hold
052: */
053: public FormatableLongHolder(long theLong) {
054: this .theLong = theLong;
055: }
056:
057: /**
058: * Set the held long to the input int.
059: *
060: * @param theLong the int to hold
061: */
062: public void setLong(int theLong) {
063: this .theLong = theLong;
064: }
065:
066: /**
067: * Get the held int.
068: *
069: * @return The held int.
070: */
071: public long getLong() {
072: return theLong;
073: }
074:
075: /**
076: * Create and return an array of FormatableLongHolders
077: * given an array of ints.
078: *
079: * @param theLongs The array of longs
080: *
081: * @return An array of FormatableLongHolders
082: */
083: public static FormatableLongHolder[] getFormatableLongHolders(
084: long[] theLongs) {
085: if (theLongs == null) {
086: return null;
087: }
088:
089: FormatableLongHolder[] flhArray = new FormatableLongHolder[theLongs.length];
090:
091: for (int index = 0; index < theLongs.length; index++) {
092: flhArray[index] = new FormatableLongHolder(theLongs[index]);
093: }
094: return flhArray;
095: }
096:
097: //////////////////////////////////////////////
098: //
099: // FORMATABLE
100: //
101: //////////////////////////////////////////////
102: /**
103: * Write this formatable out
104: *
105: * @param out write bytes here
106: *
107: * @exception IOException thrown on error
108: */
109: public void writeExternal(ObjectOutput out) throws IOException {
110: out.writeLong(theLong);
111: }
112:
113: /**
114: * Read this formatable from a stream of stored objects.
115: *
116: * @param in read this.
117: *
118: * @exception IOException thrown on error
119: */
120: public void readExternal(ObjectInput in) throws IOException {
121: theLong = in.readLong();
122: }
123:
124: public void readExternal(ArrayInputStream in) throws IOException {
125: theLong = in.readLong();
126: }
127:
128: /**
129: * Get the formatID which corresponds to this class.
130: *
131: * @return the formatID of this class
132: */
133: public int getTypeFormatId() {
134: return StoredFormatIds.FORMATABLE_LONG_HOLDER_V01_ID;
135: }
136: }
|