U8Vector.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // This file is generated from PrimVector.template. DO NOT EDIT!
  2. // Copyright (c) 2001, 2002, 2015 Per M.A. Bothner and Brainfood Inc.
  3. // This is free software; for terms and warranty disclaimer see ./COPYING.
  4. package gnu.lists;
  5. import java.io.*;
  6. import gnu.math.UByte;
  7. /** Simple adjustable-length vector of unsigned 8-bit integers (bytes). */
  8. public class U8Vector extends ByteVector<UByte>
  9. {
  10. public U8Vector() {
  11. data = empty;
  12. }
  13. public U8Vector(int size, byte value) {
  14. byte[] array = new byte[size];
  15. data = array;
  16. if (value != 0) {
  17. while (--size >= 0)
  18. array[size] = value;
  19. }
  20. }
  21. public U8Vector(int size) {
  22. this(new byte[size]);
  23. }
  24. /** Reuses the argument without making a copy. */
  25. public U8Vector(byte[] data) {
  26. this.data = data;
  27. }
  28. /** Makes a copy of (part of) the argument array. */
  29. public U8Vector(byte[] values, int offset, int length) {
  30. this(length);
  31. System.arraycopy(values, offset, data, 0, length);
  32. }
  33. public final int getIntRaw(int index) {
  34. return (int) data[index] & 0xff;
  35. }
  36. public final UByte get(int index) {
  37. return UByte.valueOf(data[effectiveIndex(index)]);
  38. }
  39. public final UByte getRaw(int index) {
  40. return UByte.valueOf(data[index]);
  41. }
  42. @Override
  43. public final void setRaw(int index, UByte value) {
  44. data[index] = value.byteValue();
  45. }
  46. @Override
  47. protected U8Vector newInstance(int newLength) {
  48. return new U8Vector(newLength < 0 ? data : new byte[newLength]);
  49. }
  50. public static U8Vector castOrNull(Object obj) {
  51. if (obj instanceof byte[])
  52. return new U8Vector((byte[]) obj);
  53. if (obj instanceof U8Vector)
  54. return (U8Vector) obj;
  55. return null;
  56. }
  57. public static U8Vector cast(Object value) {
  58. U8Vector vec = castOrNull(value);
  59. if (vec == null) {
  60. String msg;
  61. if (value == null)
  62. msg = "cannot convert null to U8Vector";
  63. else
  64. msg = "cannot convert a "+value.getClass().getName()+" to U8Vector";
  65. throw new ClassCastException(msg);
  66. }
  67. return vec;
  68. }
  69. public int getElementKind() { return INT_U8_VALUE; }
  70. public String getTag() { return "u8"; }
  71. public int compareTo(Object obj) {
  72. return compareToInt(this, (U8Vector) obj);
  73. }
  74. }