Bucket.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package oram;
  2. import exceptions.LengthNotMatchException;
  3. public class Bucket {
  4. private Tuple[] tuples;
  5. public Bucket(int numTuples) {
  6. tuples = new Tuple[numTuples];
  7. }
  8. public Bucket(Tuple[] tuples) {
  9. this.tuples = tuples;
  10. }
  11. // deep copy
  12. public Bucket(Bucket b) {
  13. tuples = new Tuple[b.getNumTuples()];
  14. for (int i = 0; i < tuples.length; i++)
  15. tuples[i] = new Tuple(b.getTuple(i));
  16. }
  17. public int getNumTuples() {
  18. return tuples.length;
  19. }
  20. public Tuple[] getTuples() {
  21. return tuples;
  22. }
  23. public Tuple getTuple(int i) {
  24. return tuples[i];
  25. }
  26. public void setTuples(Tuple[] tuples) {
  27. if (this.tuples.length != tuples.length)
  28. throw new LengthNotMatchException(this.tuples.length + " != " + tuples.length);
  29. this.tuples = tuples;
  30. }
  31. public void setTuple(int i, Tuple tuple) {
  32. tuples[i] = tuple;
  33. }
  34. public byte[] toByteArray() {
  35. int tupleBytes = tuples[0].getNumBytes();
  36. byte[] bucket = new byte[tupleBytes * tuples.length];
  37. for (int i = 0; i < tuples.length; i++) {
  38. byte[] tuple = tuples[i].toByteArray();
  39. System.arraycopy(tuple, 0, bucket, i * tupleBytes, tupleBytes);
  40. }
  41. return bucket;
  42. }
  43. @Override
  44. public String toString() {
  45. String str = "Bucket:";
  46. for (int i = 0; i < tuples.length; i++)
  47. str += ("\n " + tuples[i]);
  48. return str;
  49. }
  50. }