View Javadoc
1   /*
2    * Copyright (C) 2005-2015 Schlichtherle IT Services.
3    * All rights reserved. Use is subject to license terms.
4    */
5   package net.java.truevfs.comp.zip;
6   
7   import javax.annotation.CheckForNull;
8   import javax.annotation.concurrent.ThreadSafe;
9   
10  /**
11   * Provides constants and static utility methods for unsigned byte integer
12   * values ({@value SIZE} bits).
13   *
14   * @author Christian Schlichtherle
15   */
16  @ThreadSafe
17  final class UByte {
18  
19      /**
20       * The minimum value of an unsigned byte integer,
21       * which is {@value MIN_VALUE}.
22       */
23      public static final short MIN_VALUE = 0x00;
24  
25      /**
26       * The maximum value of an unsigned byte integer,
27       * which is {@value MAX_VALUE}.
28       */
29      public static final short MAX_VALUE = 0xff;
30  
31      /**
32       * The number of bits used to represent an unsigned short integer in
33       * binary form, which is {@value SIZE}.
34       */
35      public static final int SIZE = 8;
36  
37      private UByte() { }
38  
39      /**
40       * Checks the parameter range.
41       *
42       * @param  i The integer to check to be in the range of an unsigned byte
43       *         integer ({@value SIZE} bits).
44       * @param  subject The subject of the exception message
45       *         - may be {@code null}.
46       *         This should not end with a punctuation character.
47       * @param  error First sentence of the exception message
48       *         - may be {@code null}.
49       *         This should not end with a punctuation character.
50       * @return {@code true}
51       * @throws IllegalArgumentException If {@code i} is less than
52       *         {@link #MIN_VALUE} or greater than {@link #MAX_VALUE}.
53       */
54      public static boolean check(
55              final int i,
56              final @CheckForNull String subject,
57              final @CheckForNull String error) {
58          if (MIN_VALUE <= i && i <= MAX_VALUE) return true;
59          final StringBuilder message = new StringBuilder();
60          if (null != subject) message.append(subject).append(": ");
61          if (null != error) message.append(error).append(": ");
62          throw new IllegalArgumentException(message
63                  .append(i)
64                  .append(" is not within ")
65                  .append(MIN_VALUE)
66                  .append(" and ")
67                  .append(MAX_VALUE)
68                  .append(" inclusive.")
69                  .toString());
70     }
71  
72      /**
73       * Checks the parameter range.
74       *
75       * @param  i The integer to check to be in the range of an unsigned byte
76       *         integer ({@value SIZE} bits).
77       * @return {@code true}
78       * @throws IllegalArgumentException If {@code i} is less than
79       *         {@link #MIN_VALUE} or greater than {@link #MAX_VALUE}.
80       */
81      public static boolean check(final int i) {
82          return check(i, "Integer out of range", null);
83      }
84  }