@Test
  public void testAsList() {

    final RingBuffer<Integer> ring = new RingBufferSimple<Integer>(10);
    assertEquals(ring.length(), 10);

    ring.setHead(100, 100);

    for (int k = 100; k < 110; k++) {
      ring.set(k, k);
    }

    ring.set(102, null);
    ring.set(107, null);
    assertEquals(ring.count(), 8);

    final List<Integer> list = ring.asList();

    assertEquals(list.size(), ring.count());

    int index = 0;
    for (int k = 100; k < 110; k++) {
      final Integer ringItem = ring.get(k);
      if (ringItem == null) {
        continue;
      }
      final Integer listItem = list.get(index++);
      assertEquals(ringItem, listItem);
    }
  }
  @Test
  public void testAsMap() {

    final RingBuffer<Integer> ring = new RingBufferSimple<Integer>(10);
    assertEquals(ring.length(), 10);

    ring.setHead(100, 100);

    for (int k = 101; k < 110; k++) {
      ring.set(k, k);
    }

    ring.set(102, null);
    ring.set(107, null);
    assertEquals(ring.count(), 8);

    final Map<Integer, Integer> map = ring.asMap();

    assertEquals(map.size(), ring.count());

    // SortedMap<Integer, Integer> map1 = new TreeMap<Integer,
    // Integer>(map);

    for (int k = 100; k < 110; k++) {
      final Integer ringItem = ring.get(k);
      final Integer mapItem = map.get(k);
      assertEquals(ringItem, mapItem);
    }
  }
  @Test
  public void testAsArray() {

    final RingBuffer<Integer> ring = new RingBufferSimple<Integer>(10);
    assertEquals(ring.length(), 10);

    ring.setHead(100, 100);

    for (int k = 100; k < 110; k++) {
      ring.set(k, k);
    }

    ring.set(102, null);
    ring.set(107, null);
    assertEquals(ring.count(), 8);

    final Integer[] clone1 = ring.asArray(Integer.class);
    final Integer[] clone2 = ring.asArray(Integer.class);

    assertFalse(clone1 == clone2);
    assertTrue(Arrays.equals(clone1, clone2));

    for (int k = 0; k < 10; k++) {
      assertEquals(ring.get(100 + k), clone1[k]);
      assertEquals(ring.get(100 + k), clone2[k]);
    }
  }
  @Test
  public void testTail1() {

    final RingBuffer<Integer> ring = new RingBufferSimple<Integer>(1);
    assertEquals(ring.length(), 1);

    ring.setTail(100, 100);
    assertEquals(ring.tail(), 100);
    assertEquals(ring.get(100), (Integer) 100);

    ring.setTail(123, 100);
    assertEquals(ring.tail(), 123);
    assertEquals(ring.get(123), (Integer) 100);
  }
Exemplo n.º 5
0
 public void free(T free) {
   final ReentrantLock myLock = lock;
   myLock.lock();
   try {
     if (!pool.offer(free)) {
       discarded++;
     } else {
       pooled++;
       if (free instanceof Clearable) {
         ((Clearable) free).clear();
       }
     }
   } finally {
     myLock.unlock();
   }
 }
Exemplo n.º 6
0
 public T allocate() {
   final ReentrantLock myLock = lock;
   myLock.lock();
   try {
     T allocated = pool.poll();
     if (allocated == null) {
       created++;
       allocated = factory.newInstance(this);
     } else {
       reused++;
     }
     return allocated;
   } finally {
     myLock.unlock();
   }
 }
  @Test
  public void testRingArray1() {

    final RingBuffer<Integer> ring = new RingBufferSimple<Integer>(10);

    int ringMemSize;
    ringMemSize = JavaSize.of(ring);
    // System.err.println("ring mem size=" + ringMemSize);
    assertEquals(ringMemSize, 72);

    //

    int basis;

    for (int k = 0; k < 10; k++) {
      ring.set(k, new Integer(k));
    }

    basis = ring.head();
    // System.err.println("ring basis=" + basis);
    assertEquals(basis, 0);

    //

    for (int k = 0; k < 10; k++) {
      assertEquals(ring.get(k), (Integer) k);
    }

    //

    assertEquals(ring.head(), 0);
    assertEquals(ring.tail(), 9);
    assertEquals(ring.get(4), (Integer) 4);

    for (int k = 5; k < 10; k++) {
      // System.err.println("ring k=" + k);
      assertEquals(ring.get(k), (Integer) (k));
    }

    //

  }
Exemplo n.º 8
0
 public int size() {
   return pool.size();
 }
 @Test(expected = ArrayIndexOutOfBoundsException.class)
 public void testSetException2() {
   final RingBuffer<Integer> ring = new RingBufferSimple<Integer>(10);
   ring.setHead(100, 100);
   ring.set(110, 110);
 }
  @Test
  public void testyTail() {

    final RingBuffer<Integer> ring = new RingBufferSimple<Integer>(10);
    assertEquals(ring.length(), 10);

    ring.setTail(109, 109);
    assertEquals(ring.count(), 1);
    assertEquals(ring.get(109), (Integer) 109);

    for (int k = 100; k < 109; k++) {
      assertEquals(ring.get(k), null);
    }

    for (int k = 100; k < 110; k++) {
      ring.set(k, k);
    }

    assertEquals(ring.count(), 10);

    for (int k = 100; k < 110; k++) {
      assertEquals(ring.get(k), (Integer) k);
    }

    ring.setTail(104, 204);
    assertEquals(ring.count(), 5);
    assertEquals(ring.head(), 95);
    assertEquals(ring.tail(), 104);
    assertEquals(ring.get(104), (Integer) 204);

    for (int k = 100; k < 104; k++) {
      assertEquals(ring.get(k), (Integer) k);
    }

    for (int k = 95; k < 100; k++) {
      assertEquals(ring.get(k), (Integer) null);
    }

    ring.set(95, 95);
    assertEquals(ring.get(95), (Integer) 95);
    assertEquals(ring.count(), 6);
  }
  @Test
  public void testHead() {

    final RingBuffer<Integer> ring = new RingBufferSimple<Integer>(10);
    assertEquals(ring.length(), 10);

    ring.setHead(100, 100);
    assertEquals(ring.count(), 1);
    assertEquals(ring.get(100), (Integer) 100);

    for (int k = 101; k < 110; k++) {
      assertEquals(ring.get(k), null);
    }

    for (int k = 100; k < 110; k++) {
      ring.set(k, k);
    }

    assertEquals(ring.count(), 10);

    for (int k = 100; k < 110; k++) {
      assertEquals(ring.get(k), (Integer) k);
    }

    ring.setHead(105, 205);

    assertEquals(ring.count(), 5);
    assertEquals(ring.head(), 105);
    assertEquals(ring.tail(), 114);

    assertEquals(ring.get(105), (Integer) 205);

    for (int k = 106; k < 110; k++) {
      assertEquals(ring.get(k), (Integer) k);
    }

    for (int k = 110; k < 115; k++) {
      assertEquals(ring.get(k), (Integer) null);
    }

    ring.set(114, 114);

    assertEquals(ring.count(), 6);
    assertEquals(ring.get(114), (Integer) 114);
  }
Exemplo n.º 12
0
  public RBootsWallSprite(RBootsView host, Sprite bg) {
    super(host, theWallImg);
    _bg = bg;
    _prevX = 0.0f;
    _stopped = false;
    _topEdges = new RingBuffer(200);
    _bottomEdges = new RingBuffer(200);
    SpriteAppearanceAgent a =
        new SpriteAppearanceAgent() {
          public void renderSpriteOn(Sprite aSprite, Canvas aCanvas) {
            PointF p = pos();
            int le = (int) _leftEdge;
            int i = 0;
            while (le < aCanvas.getWidth() + WALL_SEGMENT_WIDTH) {
              aCanvas.drawBitmap(
                  shape(), le, _topEdges.ref(i) - shape().getHeight(), aSprite.host().paint());
              aCanvas.drawBitmap(shape(), le, _bottomEdges.ref(i), aSprite.host().paint());
              i++;
              le += WALL_SEGMENT_WIDTH;
            }
          }
        };

    SpriteBehaviorAgent b =
        new SpriteBehaviorAgent() {
          public void act(Sprite aSprite) {
            RBootsView v = (RBootsView) host();
            if (!v.isPaused()) {
              while (_xPos > pos().x) {
                _topEdges.append(nextWallTop());
                _bottomEdges.append(nextWallBottom());
                _xPos -= WALL_SEGMENT_WIDTH;
              }
              _leftEdge = pos().x;
              while (_leftEdge < -WALL_SEGMENT_WIDTH) {
                _leftEdge += WALL_SEGMENT_WIDTH;
              }
              _bg.moveTo(_bg.pos().x + (vel().x / 2.0f), _bg.pos().y + (vel().y / 2.0f));
              v.updateScoar((int) (-(pos().x) / 20.0f));
              if (v.ogen() != null) {
                v.ogen().update(-(pos().x));
              }
              if (!_stopped && ((int) _prevX / 1200) != ((int) (pos().x) / 1200)) {
                setVel(vel().x * 1.2f, 0.0f);
              }
              _prevX = pos().x;
              DefaultBehaviorAgent.instance().act(aSprite);
            }
          }
        };
    setAppearanceAgent(a);
    setBehaviorAgent(b);
    _wallTop = 15;
    _wallTopFirstDeriv = 0;
    _wallTopSecondDeriv = 0;
    _wallDist = 200;
    _xPos = 0.0;
    _leftEdge = 0.0;
    for (int i = 0; i < 200; i++) {
      _topEdges.append(nextWallTop());
      _bottomEdges.append(nextWallBottom());
    }
    setVel(-1.0f, 0.0f);
  }
Exemplo n.º 13
0
 public int bottomEdgeFor(float x) {
   int idx = (int) (x + _leftEdge);
   return _bottomEdges.ref(idx / WALL_SEGMENT_WIDTH);
 }
Exemplo n.º 14
0
 private void addToBuffer(int i) {
   transactionRingBuffer.push(transactions[i]);
 }
Exemplo n.º 15
0
  public synchronized void run() {
    lastTimeCalc = System.currentTimeMillis();
    long startTime = lastTimeCalc;

    long timeDifference =
        -1; // time difference between current time (the time replay started) and the timestamp of
            // the first package

    while (buffer.getCurrentHeader() == null) {
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    String header = buffer.getCurrentHeader();
    String[] headerData = header.split(",");
    duration = Long.parseLong(headerData[1]);

    if (headerData.length > 2) {
      totalPauseTime = Long.parseLong(headerData[2]);
    }

    if (recorder.getReplayMode() == DataRecorder.REPLAYMODE_DEFAULT) {
      duration = duration - totalPauseTime;
    }

    while (keepAlive) {
      while (pause) {
        try {
          calculateTimeOver(System.currentTimeMillis());
          wait();
          lastTimeCalc = System.currentTimeMillis();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      LoggingObject l = null;
      try {
        l = buffer.dequeue();
      } catch (InterruptedException e) {
      }
      if (l == null) {
        recorder.playerHasfinished();
        break;
      }
      if (timeDifference == -1) {
        timeDifference = startTime - l.getTimestamp();
      }

      // Depending on replay mode, ignore pause or not
      if (l.getType() == LoggingObject.TYPE_PAUSE
          && recorder.getReplayMode() == DataRecorder.REPLAYMODE_DEFAULT) {
        if (pauseStarted == -1) {
          pauseStarted = l.getTimestamp();
        } else {
          currentPauseTime += l.getTimestamp() - pauseStarted;
          pauseStarted = -1;
        }
        continue;
      }

      while ((l.getTimestamp() + timeDifference - currentPauseTime) > (startTime + timePlayed)) {
        while (pause) {
          try {
            calculateTimeOver(System.currentTimeMillis());
            wait();
            lastTimeCalc = System.currentTimeMillis();
            timeDifference = System.currentTimeMillis() - l.getTimestamp(); //
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }

        calculateTimeOver(System.currentTimeMillis());

        float percentagePlayed = (float) timePlayed / duration;
        recorder.updateGUI(percentagePlayed, timePlayed - TIMESTAMP_SUBTRACT_CORRECTION);
      }
      calculateTimeOver(System.currentTimeMillis());
      float percentagePlayed = (float) timePlayed / duration;
      recorder.updateGUI(percentagePlayed, timePlayed);

      if (l.getType() == LoggingObject.TYPE_DATA) recorder.doPublish(l.getDataContainer());

      if (singleStep) {
        pause = true;
      }
    }
    // when reaching this part, the player has either been stopped or the file has ended. Clear the
    // buffer
    buffer = null;
  }