View Javadoc

1   /*
2    * Copyright (c) 2005 The University of Reading
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    * 1. Redistributions of source code must retain the above copyright
9    *    notice, this list of conditions and the following disclaimer.
10   * 2. Redistributions in binary form must reproduce the above copyright
11   *    notice, this list of conditions and the following disclaimer in the
12   *    documentation and/or other materials provided with the distribution.
13   * 3. Neither the name of the University of Reading, nor the names of the
14   *    authors or contributors may be used to endorse or promote products
15   *    derived from this software without specific prior written permission.
16   * 
17   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   */
28  
29  package uk.ac.rdg.resc.jstyx.types;
30  
31  import java.math.BigInteger;
32  
33  /***
34   * Unsigned long integer
35   *
36   * @author Jon Blower
37   * $Revision: 311 $
38   * $Date: 2005-07-08 13:18:50 +0100 (Fri, 08 Jul 2005) $
39   * $Log$
40   * Revision 1.2  2005/07/08 12:18:50  jonblower
41   * Implemented equals() methods
42   *
43   * Revision 1.1.1.1  2005/02/16 18:58:35  jonblower
44   * Initial import
45   *
46   */
47  public class ULong
48  {    
49      private static final int ULONG_LENGTH = 8;
50      public static final ULong ZERO = new ULong(0);
51      
52      private byte[] bytes;
53      
54      /***
55       * Constructs a ULong from an array of 8 bytes in little-endian order
56       * @throws IllegalArgumentException if the array is not 8 bytes long or if 
57       * it is null
58       */
59      public ULong(byte[] b)
60      {
61          if (b == null)
62          {
63              throw new IllegalArgumentException("Input array of bytes cannot be null");
64          }
65          if (b.length != ULONG_LENGTH)
66          {
67              throw new IllegalArgumentException("ULong must be "
68                  + ULONG_LENGTH + " bytes long");
69          }
70          this.bytes = b;
71      }
72      
73      public ULong(long l)
74      {        
75          this.bytes = new byte[ULONG_LENGTH];
76          this.setValue(l);
77      }
78      
79      public BigInteger asBigInteger()
80      {
81          // Need to reverse the array to get into big-endian order
82          byte[] revBytes = new byte[8];
83          for (int i = 0; i < ULONG_LENGTH; i++)
84          {
85              revBytes[i] = this.bytes[ULONG_LENGTH - 1 - i];
86          }
87          return new BigInteger(revBytes);
88      }
89      
90      public long asLong()
91      {
92          return this.asBigInteger().longValue();
93      }
94      
95      public byte[] getBytes()
96      {
97          return bytes;
98      }
99      
100     public void setValue(long l)
101     {
102         this.bytes[0] = (byte)l;
103         this.bytes[1] = (byte)(l >> 8);
104         this.bytes[2] = (byte)(l >> 16);
105         this.bytes[3] = (byte)(l >> 24);
106         this.bytes[4] = (byte)(l >> 32);
107         this.bytes[5] = (byte)(l >> 40);
108         this.bytes[6] = (byte)(l >> 48);
109         this.bytes[7] = (byte)(l >> 56);        
110     }
111     
112     public String toString()
113     {
114         return this.asBigInteger().toString();
115     }
116     
117     public boolean equals(Object otherULong)
118     {
119         if (otherULong == null)
120         {
121             return false;
122         }
123         else if (otherULong instanceof ULong)
124         {
125             ULong ul2 = (ULong)otherULong;
126             // compare byte-by-byte
127             for (int i = 0; i < ULONG_LENGTH; i++)
128             {
129                 if (this.bytes[i] != ul2.bytes[i])
130                 {
131                     return false;
132                 }
133             }
134             return true;
135         }
136         else
137         {
138             return false;
139         }
140     }
141     
142     /***
143      * TODO: implement hashCode()
144      */
145     
146 }