Project Report: fawkez

Packagesummary org.jcoderz.guidelines.snippets

org.jcoderz.guidelines.snippets.TransactionId

LineHitsNoteSource
1 (1)// BEGIN SNIPPET: JCoderZJavaExample.xml
2  /*
3   * $Id: TransactionId.java 1011 2008-06-16 17:57:36Z amandel $
4   *
5   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
6   *
7   * Redistribution and use in source and binary forms, with or without
8   * modification, are permitted provided that the following conditions are
9   * met:
10   *
11   *    * Redistributions of source code must retain the above copyright
12   *      notice, this list of conditions and the following disclaimer.
13   *    * Redistributions in binary form must reproduce the above
14   *      copyright notice, this list of conditions and the following
15   *      disclaimer in the documentation and/or other materials
16   *      provided with the distribution.
17   *    * Neither the name of the jCoderZ.org Project nor the names of
18   *      its contributors may be used to endorse or promote products
19   *      derived from this software without specific prior written
20   *      permission.
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
23   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
26   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   */
34  package org.jcoderz.guidelines.snippets;
35  
36  
37  import java.io.File;
38  import java.io.FileInputStream;
39  import java.io.IOException;
40  import java.io.Serializable;
41  
42  
43  /**
44   * This util class represents an Transaction Id with all
45   * of its features and restrictions.
46   * Instances of this class are immutable.
47   *
48   * @author SWAG
49   */
50  public final class TransactionId
51      implements Comparable, Serializable
52  {
53      /** Name of this type. */
54      public static final String TYPE_NAME = "TX_ID";
55  
56      /** Bit mask used for hashcode generation. */
57      private static final int NUMBER_OF_BITS_PER_INT = 32;
58  
59      private static final int BUFFER_SIZE = 4096;
60  
61      private static final int BUFFER_MULTIPLIER = 2;
62  
63      private static final long serialVersionUID = -7064645359225861305L;
64  
65      /** Holds the transaction id */
66      private final long mTransactionId;
67  
68  
69      /**
70       * Creates a new instance of TransactionId.
71       *
72       * @param transactionId the transaction to be represented by the
73       *       <code>TransactionId</code>.
74       * @throws IllegalArgumentException if the long does not fit
75       *       into a transaction id.
76       */
77      private TransactionId (long transactionId)
78          throws IllegalArgumentException
790     {
800         if (transactionId < 0)
81          {
820             throw new IllegalArgumentException(TYPE_NAME + " "
83                      + String.valueOf(transactionId)
84                      + "Value must be positive.");
85          }
860         mTransactionId = transactionId;
870     }
88  
89  
90      /**
91       * Parses the string argument as a transaction id.
92       *
93       * @param s the <code>String</code> containing the transaction id.
94       * @return the transaction id represented by the string argument.
95       * @throws IllegalArgumentException if the string does not contain a
96       *       parseable transaction id.
97       */
98      public static TransactionId fromString (String s)
99          throws IllegalArgumentException
100      {
101          final TransactionId result;
102          try
103          {
1040             result = new TransactionId(Long.parseLong(s));
105          }
1060         catch (NumberFormatException ex)
107          {
1080             final IllegalArgumentException iaex
109                      = new IllegalArgumentException(
110                          TYPE_NAME + " Failed to parse the value.");
1110             iaex.initCause(ex);
1120(2)            throw iaex;
113          }
1140(3)        catch (NullPointerException ex)
115          {
1160             final IllegalArgumentException iaex
117                      = new IllegalArgumentException(
118                          TYPE_NAME + " Value must not be null.");
1190             iaex.initCause(ex);
1200(4)            throw iaex;
1210         }
1220         return result;
123      }
124  
125      /**
126       * Returns a transaction id from the given long <code>l</code>.
127       *
128       * @param l the <code>long</code> containing the transaction id.
129       * @return the transaction id represented by the argument.
130       * @throws IllegalArgumentException if the long does not fit into a
131       *       transaction id.
132       */
133      public static TransactionId fromLong (long l)
134          throws IllegalArgumentException
135      {
1360         return new TransactionId(l);
137      }
138  
139      /**
140       * Returns the transaction id as String.
141       *
142       * @return the transaction id as String.
143       */
144      public String toString ()
145      {
1460         return Long.toString(mTransactionId);
147      }
148  
149      /**
150       * Returns the transaction id as long.
151       *
152       * @return the transaction id as long.
153       */
154      public long toLong ()
155      {
1560         return mTransactionId;
157      }
158  
159      /**
160       * Indicates whether some other object is "equal to" this one.
161       *
162       * @param obj the object to compare this <code>TransactionId</code>
163       *       against.
164       * @return true if this object is the same as the obj argument; false
165       *       otherwise.
166       */
167      public boolean equals (Object obj)
168      {
1690         boolean result = false;
170  
1710         if (obj instanceof TransactionId)
172          {
1730             result = (mTransactionId
174                      == (((TransactionId) obj).mTransactionId));
175          }
176  
1770         return result;
178      }
179  
180      /**
181       * Compare two transaction IDs.
182       * This implementation is consistent with {@link #equals(Object)}.
183       *
184       * @param o object with which to compare this TransactionId
185       * @return a result less than zero if this object is less than
186       *       <code>o</code>, exactly zero if they are equal, and a result
187       *       greater than zero otherwise.
188       */
189      public int compareTo (Object o)
190      {
191          final int result;
192          // Can't simply return the difference, because that difference
193          // might not fit in an int.
1940         if (mTransactionId < ((TransactionId) o).mTransactionId)
195          {
1960             result = -1;
197          }
1980         else if (mTransactionId > ((TransactionId) o).mTransactionId)
199          {
2000             result = 1;
201          }
202          else
203          {
2040             result = 0;
205          }
2060         return result;
207      }
208  
209      /**
210       * Compute hash code.
211       *
212       * @return hash code for this transaction ID
213       */
214      public int hashCode ()
215      {
2160         return (int) (mTransactionId
217                  ^ (mTransactionId >>> NUMBER_OF_BITS_PER_INT));
218      }
219  
220      /**
221       * Helper function to read the full content of the file.
222       *
223       * @param file the file to read.
224       * @return the content of the given file as byte array.
225       * @throws IOException if a IOException occurs.
226       */
227 (5)    private static byte[] readFully (File file)
228          throws IOException
229      {
2300(6)        final FileInputStream in = new FileInputStream(file);
2310         byte[] buffer = new byte[BUFFER_SIZE];
232          int read;
2330         int pos = 0;
234  
2350         while ((read = in.read(buffer, pos, buffer.length - pos)) > 0)
236          {
2370             pos += read;
2380             if (pos == buffer.length)
239              {
2400(7)                byte[] newBuffer
241                  = new byte[buffer.length * BUFFER_MULTIPLIER];
242  
2430                 System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
2440                 buffer = newBuffer;
2450             }
246          }
247  
2480         if (pos != buffer.length)
249          {
2500(8)            byte[] newBuffer = new byte[pos];
251  
2520             System.arraycopy(buffer, 0, newBuffer, 0, pos);
2530             buffer = newBuffer;
254          }
255  
2560         return buffer;
257      }
258  }
259  // END SNIPPET

Findings in this File

c (1) 1 : 0 Line does not match expected header line of '^/\*$'.
i (2) 112 : 0 method org.jcoderz.guidelines.snippets.TransactionId.fromString(String) throws exception with static message string
d (3) 114 : 16 Avoid catching NullPointerException; consider removing the cause of the NPE.
i (4) 120 : 0 method org.jcoderz.guidelines.snippets.TransactionId.fromString(String) throws exception with static message string
i (5) 227 : 27 Avoid unused private methods such as 'readFully(File)'.
w (6) 230 : 0 org.jcoderz.guidelines.snippets.TransactionId.readFully(File) may fail to close stream
c (7) 240 : 24 Variable 'newBuffer' should be declared final.
c (8) 250 : 20 Variable 'newBuffer' should be declared final.