View Javadoc
1   package org.djutils.serialization.serializers;
2   
3   /**
4    * Container for an offset.
5    * <p>
6    * Copyright (c) 2019-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
7    * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
8    * </p>
9    * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
10   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
11   */
12  public class Pointer
13  {
14      /** Current value of the offset. */
15      private int offset;
16  
17      /**
18       * Construct a new Pointer with specified initial offset.
19       * @param initialOffset int; the initial offset
20       */
21      Pointer(final int initialOffset)
22      {
23          this.offset = initialOffset;
24      }
25  
26      /**
27       * Construct a new Pointer with offset 0.
28       */
29      public Pointer()
30      {
31          this(0);
32      }
33  
34      /**
35       * Retrieve the offset.
36       * @return int; the offset
37       */
38      public int get()
39      {
40          return this.offset;
41      }
42  
43      /**
44       * Retrieve the current value of offset and increment it. The returned value is the value <b>before</b> applying the
45       * increment.
46       * @param increment int; the amount by which the offset must be incremented
47       * @return int; the offset (before the increment was added)
48       */
49      public int getAndIncrement(final int increment)
50      {
51          int result = this.offset;
52          this.offset += increment;
53          return result;
54      }
55  
56      /**
57       * Increment the offset.
58       * @param increment int; the amount by which the offset must be incremented
59       */
60      public void inc(final int increment)
61      {
62          this.offset += increment;
63      }
64  
65      @Override
66      public String toString()
67      {
68          return "Pointer [offset=" + this.offset + "]";
69      }
70  
71  }