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