Source for gnu.CORBA.Version

   1: /* Version.java --
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.CORBA;
  40: 
  41: import java.io.IOException;
  42: import java.io.Serializable;
  43: 
  44: import org.omg.CORBA.MARSHAL;
  45: 
  46: /**
  47:  * A version number, represented by the major version number
  48:  * and the minor version number.
  49:  *
  50:  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  51:  */
  52: public class Version
  53:   implements Serializable
  54: {
  55:   /**
  56:    * Use serialVersionUID for interoperability.
  57:    */
  58:   private static final long serialVersionUID = 1;
  59: 
  60:   /**
  61:    * Major number (0..256, so the byte cannot be used).
  62:    */
  63:   public final int major;
  64: 
  65:   /**
  66:    * Minor number.
  67:    */
  68:   public final int minor;
  69: 
  70:   /**
  71:    * Create the version with the given version numbers.
  72:    *
  73:    * @param _major major number (0..255)
  74:    * @param _minor minor number (0..255)
  75:    */
  76:   public Version(int _major, int _minor)
  77:   {
  78:     major = (byte) _major;
  79:     minor = (byte) _minor;
  80:   }
  81: 
  82:   /**
  83:    * Returns true if the versions are equal.
  84:    * @param other the other version to compare.
  85:    *
  86:    * @return true if the versions are equal
  87:    */
  88:   public boolean equals(java.lang.Object other)
  89:   {
  90:     if (other == this)
  91:       {
  92:         return true;
  93:       }
  94:     if (!(other instanceof Version))
  95:       {
  96:         return false;
  97:       }
  98: 
  99:     Version that = (Version) other;
 100:     return same(that);
 101:   }
 102: 
 103:   /**
 104:    * Get the hashcode, higher 8 bits being the major version and lower 8 bits
 105:    * the minor version.
 106:    */
 107:   public int hashCode()
 108:   {
 109:     return major << 8 | minor;
 110:   }
 111: 
 112:   /**
 113:    * Read from the input stream, major number first.
 114:    * @param in a stream to read from.
 115:    */
 116:   public static Version read_version(java.io.InputStream in)
 117:   {
 118:     try
 119:       {
 120:         int major = in.read() & 0xFF;
 121:         int minor = in.read() & 0xFF;
 122:         return new Version(major, minor);
 123:       }
 124:     catch (IOException ex)
 125:       {
 126:         MARSHAL m = new MARSHAL("IOException while reading message header");
 127:         m.initCause(ex);
 128:         m.minor = Minor.Header;
 129:         throw m;
 130:       }
 131:   }
 132: 
 133:   /**
 134:    * Returns true if the versions are the same.
 135:    *
 136:    * @param that the other version to compare.
 137:    *
 138:    * @return true if the versions are the same.
 139:    */
 140:   public boolean same(Version that)
 141:   {
 142:     return major == that.major && minor == that.minor;
 143:   }
 144: 
 145:   /**
 146:    * Returns true if the given version is higher than
 147:    * or equals to the version, supplied as parameter
 148:    * in the form of two integers.
 149:    *
 150:    * @param a_major major number of the version to compare.
 151:    * @param a_minor minor number of the version to compare.
 152:    *
 153:    * @return true if this version is higher than or equals to
 154:    * the version v.
 155:    */
 156:   public boolean since_inclusive(int a_major, int a_minor)
 157:   {
 158:     if (major > a_major)
 159:       return true;
 160:     else if (major < a_major)
 161:       return false;
 162:     else
 163: 
 164:       // Major numbers are equal.
 165:       return minor >= a_minor;
 166:   }
 167: 
 168:   /**
 169:    * Return the string representation, in the form
 170:    * major.minor.
 171:    */
 172:   public String toString()
 173:   {
 174:     return major + "." + minor;
 175:   }
 176: 
 177:   /**
 178:    * Returs true if the given version is lower or equal to the
 179:    * version, specified by the provided minor and major version
 180:    * number. This means, the version, specified by these two numbers,
 181:    * should be supported by the current version.
 182:    *
 183:    * @param a_major a major version number.
 184:    * @param a_minor a minor version number.
 185:    *
 186:    * @return true if the current version should be supported by the
 187:    * version, specified by the two passed numbers.
 188:    */
 189:   public boolean until_inclusive(int a_major, int a_minor)
 190:   {
 191:     if (major < a_major)
 192:       return true;
 193:     else if (major > a_major)
 194:       return false;
 195:     else
 196: 
 197:       // Major numbers are equal.
 198:       return minor <= a_minor;
 199:   }
 200: 
 201:   /**
 202:    * Write into the output stream, major number first.
 203:    *
 204:    * @param out a stream to write into.
 205:    */
 206:   public void write(java.io.OutputStream out)
 207:   {
 208:     try
 209:       {
 210:         out.write(major & 0xFF);
 211:         out.write(minor & 0xFF);
 212:       }
 213:     catch (IOException ex)
 214:       {
 215:         MARSHAL m = new MARSHAL("IOException while writing message header");
 216:         m.minor = Minor.Header;
 217:         m.initCause(ex);
 218:         throw m;
 219:       }
 220:   }
 221: 
 222: }