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