Exemple #1
0
    /**
     * Takes an instance of Buffer and checks the class name to see if it contains the class name of
     * a Buffer base class. For example: InstanceName.contains(SimpleName); // where -> InstanceName
     * = "java.nio.DirectFloatBufferS"; SimpleName = "FloatBuffer"; // Enumeration value class name
     *
     * @param <B> the getType of buffer as input
     * @param buffer the buffer to check getType
     * @return BufferType if match found, null if not
     */
    public static <B extends Buffer> BufferType getType(B buffer) {
      if (buffer == null) {
        throw new NullPointerException("Buffer is null.");
      }
      BufferType type = null;
      String instanceClassName = buffer.getClass().getName();

      // Check if buffer class name contains the simple enum name
      for (BufferType t : BufferType.values()) {
        if (instanceClassName.contains(t.BUFFER_CLASS.getSimpleName())) {
          type = t;
          break;
        }
      }
      return type;
    }
Exemple #2
0
    /**
     * Gets BufferType given a Number. User should check for null return.
     *
     * @param <N> the getType of value, a Number
     * @param value the Number to match against enum values
     * @return BufferType if match is found, null if not
     * @throws NullPointerException
     */
    public static <N extends Number> BufferType getType(N value) {
      if (value == null) {
        throw new NullPointerException();
      }
      BufferType type = null;
      String instanceClassName = value.getClass().getName();

      for (BufferType t : BufferType.values()) {
        if (instanceClassName.contains(t.VALUE_CLASS.getSimpleName())) {
          type = t;
          break;
        }
      }

      if (type == null) {
        throw new NullPointerException();
      }

      return type;
    }
Exemple #3
0
  /**
   * Assigns the data to the buffer. This is the bare minimum constructor. Calls super constructor
   * for setting all values that weren't defined here.
   *
   * @param data the data to store into the buffer
   */
  public GLBuffer(ArrayList<N> data) {
    this.glBindType = DEFAULT_BIND;
    this.glDrawType = DEFAULT_DRAW;

    // Setup the buffer data
    try {
      if (data == null) {
        throw new NullPointerException();
      } else if (data.size() < 1) {
        throw new IllegalArgumentException("Data list is zero length.");
      }
      this.type = BufferType.getType(data.get(0));
      this.byteSize = type.getByteSize();

      // Default to empty buffer
    } catch (IllegalArgumentException | NullPointerException ex) {
      System.err.println(ex.toString() + "Creating empty default buffer...");
      // Create empty buffer
      data = new ArrayList<>();

      // Set getType of buffer by checking the instantiated variable
      N val = (N) (Number) 1;
      this.type = BufferType.getType(val);

      // Get byte size from getType
      this.byteSize = type.getByteSize();

      // Create the buffer with the setup data
      // Probably should NOT put this code into finally but it's so convenient
    } finally {
      this.buffer = ByteBuffer.allocateDirect(data.size() * this.byteSize);
      for (Number n : data) {
        switch (this.type) {
          case BYTE_BUFFER:
            this.buffer.put((Byte) n);
            break;
          case DOUBLE_BUFFER:
            this.buffer.putDouble((Double) n);
            break;
          case FLOAT_BUFFER:
            this.buffer.putFloat((Float) n);
            break;
          case INT_BUFFER:
            this.buffer.putInt((Integer) n);
            break;
          case SHORT_BUFFER:
            this.buffer.putShort((Short) n);
            break;
          default:
            // Should NOT execute. If it does, something's broke.
            // System.err.println("Something's broke.");;
            System.err.println(this.type.toString() + " does not match any predefined type.");
        }
      }
      this.buffer.flip();
      this.bufferSize = this.buffer.capacity() / this.byteSize;
      if (this.bufferSize != data.size()) {
        System.err.println("Data was not transferred correctly to buffer.");
      }
    }
  }