Version.java 5.7 KB

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