1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.millscript.commons.util.list;
22
23 import java.io.Serializable;
24
25 import org.millscript.commons.util.IList;
26 import org.millscript.commons.util.ListIterator;
27 import org.millscript.commons.util.iterator.VariableLengthListIterator;
28
29 /**
30 * This class provides an immutable <code>List</code> implementation which
31 * contains a single object.
32 */
33 public class ISingletonList< V > extends AbstractIList< V > implements Cloneable, Serializable {
34
35 /**
36 * This is the ID from the release 0.1.0 for future compatibility.
37 */
38 private static final long serialVersionUID = 8095301075490721350L;
39
40 /**
41 * The single value contained in this list.
42 */
43 private final V store;
44
45 /**
46 * Constructs a new immutable singleton list with the specified object as
47 * its single element.
48 *
49 * @param value the single object for this list
50 */
51 public ISingletonList( final V value ) {
52 this.store = value;
53 }
54
55 /**
56 * @see java.lang.Object#clone()
57 */
58 @Override
59 public Object clone() throws CloneNotSupportedException {
60
61 return super.clone();
62 }
63
64 /**
65 * @see org.millscript.commons.util.list.AbstractIList#doGet(int)
66 */
67 @Override
68 protected V doGet( final int pos ) {
69 return this.store;
70 }
71
72 /**
73 * @see org.millscript.commons.util.list.AbstractIList#doSlice(int, int, boolean)
74 */
75 @Override
76 protected IList< V > doSlice( final int first, final int last, final boolean share ) {
77
78
79
80
81 return this;
82 }
83
84 /**
85 * @see org.millscript.commons.util.IList#indexOf(java.lang.Object)
86 */
87 public int indexOf( final V value ) {
88 if ( value == null ? this.store == null : value.equals( this.store ) ) {
89 return 1;
90 } else {
91 return 0;
92 }
93 }
94
95 /**
96 * @see org.millscript.commons.util.IMap#iterator(boolean)
97 */
98 public ListIterator< V > iterator( final boolean share ) {
99
100
101 return new VariableLengthListIterator< V >( 1, this.store );
102 }
103
104 /**
105 * @see org.millscript.commons.util.IMap#size()
106 */
107 public int size() {
108 return 1;
109 }
110
111 }