package com.io7m.jcanephora;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;

import com.io7m.jaux.Constraints;
import com.io7m.jaux.Constraints.ConstraintError;

/**
 * An immutable reference to an allocated index buffer.
 */

@Immutable public final class IndexBuffer implements Buffer, GLResource
{
  private final int            value;
  private final long           elements;
  private final GLUnsignedType type;

  IndexBuffer(
    final int value,
    final long elements,
    final GLUnsignedType type)
    throws ConstraintError
  {
    this.value =
      Constraints.constrainRange(
        value,
        0,
        Integer.MAX_VALUE,
        "buffer ID value");
    this.elements =
      Constraints.constrainRange(elements, 1, Integer.MAX_VALUE, "elements");
    this.type = Constraints.constrainNotNull(type, "GL type");
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#toString()
   */

  @Override public void delete(
    final @Nonnull GLInterface gl)
    throws ConstraintError,
      GLException
  {
    Constraints.constrainNotNull(gl, "OpenGL interface");
    gl.deleteIndexBuffer(this);
  }

  @Override public long getElements()
  {
    return this.elements;
  }

  @Override public long getElementSizeBytes()
  {
    switch (this.type) {
      case TYPE_UNSIGNED_BYTE:
        return 1;
      case TYPE_UNSIGNED_INT:
        return 4;
      case TYPE_UNSIGNED_SHORT:
        return 2;
    }

    throw new AssertionError();
  }

  @Override public int getLocation()
  {
    return this.value;
  }

  @Override public long getSizeBytes()
  {
    return this.getElementSizeBytes() * this.elements;
  }

  public @Nonnull GLUnsignedType getType()
  {
    return this.type;
  }

  @Override public String toString()
  {
    final StringBuilder builder = new StringBuilder();
    builder.append("[ArrayBufferID ");
    builder.append(this.value);
    builder.append(" ");
    builder.append(this.elements);
    builder.append(" ");
    builder.append(this.getElementSizeBytes());
    builder.append("]");

    return builder.toString();
  }
}

@@ -1144,32 +1144,97 @@
 fer(this);%0A  %7D%0A%0A
+  /**%0A   * Retrieve the number of elements in the buffer.%0A   */%0A%0A
   @Override publ
@@ -1286,24 +1286,86 @@
 ments;%0A  %7D%0A%0A
+  /**%0A   * Retrieve the size in bytes of each element.%0A   */%0A%0A
   @Override 
@@ -1624,73 +1624,217 @@
 %0A%0A  
-@Override public int getLocation()%0A  %7B%0A    return this.value;%0A  %7D
+/**%0A   * Retrieve the raw OpenGL 'location' of the buffer.%0A   */%0A%0A  @Override public int getLocation()%0A  %7B%0A    return this.value;%0A  %7D%0A%0A  /**%0A   * Retrieve the total size in bytes of the allocated buffer.%0A   */
 %0A%0A  
@@ -1934,16 +1934,83 @@
 s;%0A  %7D%0A%0A
+  /**%0A   * Retrieve the type of the elements in the buffer.%0A   */%0A%0A
   public

Update javadoc