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.alert.alerts.IllegalArgumentAlert;
26 import org.millscript.commons.util.IList;
27 import org.millscript.commons.util.ListIterator;
28 import org.millscript.commons.util.iterator.VariableLengthListIterator;
29
30 /**
31 * This class implements a variable length list which has the same value for
32 * each element.
33 */
34 public class IVarLengthList< V > extends AbstractIList< V > implements Cloneable, Serializable {
35
36 /**
37 * This is the ID from the release 0.1.0 for future compatibility.
38 */
39 private static final long serialVersionUID = -9221933010435703703L;
40
41 /**
42 * The value to use for each value in the list.
43 */
44 private final V store;
45
46 /**
47 * The size of the list.
48 */
49 private final int size;
50
51 /**
52 * Constructs a new variable length list with the specified size and object
53 * to use for each value in it.
54 *
55 * @param length the size of the list
56 * @param value the value for each element
57 */
58 public IVarLengthList( final int length, final V value ) {
59 if ( length < 0 ) {
60 throw new IllegalArgumentAlert(
61 "Can't construct a list of negative length"
62 ).culprit(
63 "requested length",
64 length
65 ).culprit(
66 "list value",
67 value
68 ).mishap();
69 }
70 this.size = length;
71 this.store = value;
72 }
73
74 /**
75 * @see java.lang.Object#clone()
76 */
77 @Override
78 public Object clone() throws CloneNotSupportedException {
79
80 return super.clone();
81 }
82
83 /**
84 * @see org.millscript.commons.util.list.AbstractIList#doGet(int)
85 */
86 @Override
87 protected V doGet( final int pos ) {
88 return this.store;
89 }
90
91 /**
92 * @see org.millscript.commons.util.list.AbstractIList#doSlice(int, int, boolean)
93 */
94 @Override
95 protected IList< V > doSlice( final int first, final int last, final boolean share ) {
96
97
98 return new IVarLengthList< V >( last - first + 1, this.store );
99 }
100
101 /**
102 * @see org.millscript.commons.util.IList#indexOf(java.lang.Object)
103 */
104 public int indexOf( final V value ) {
105 if ( this.size != 0 ) {
106 if ( value == null ? this.store == null : value.equals( this.store ) ) {
107 return 1;
108 }
109 }
110 return 0;
111 }
112
113 /**
114 * @see org.millscript.commons.util.IMap#iterator(boolean)
115 */
116 public ListIterator< V > iterator( final boolean share ) {
117
118
119 return new VariableLengthListIterator< V >( this.size, this.store );
120 }
121
122 /**
123 * @see org.millscript.commons.util.IMap#size()
124 */
125 public int size() {
126 return this.size;
127 }
128
129 }