Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_903+3D / applications / appgvSIG / src / com / vividsolutions / jump / util / ImmutableFirstElementList.java @ 10722

History | View | Annotate | Download (4.15 KB)

1
/*
2
 * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI 
3
 * for visualizing and manipulating spatial features with geometry and attributes.
4
 *
5
 * Copyright (C) 2003 Vivid Solutions
6
 * 
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 * 
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 * 
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
 * 
21
 * For more information, contact:
22
 *
23
 * Vivid Solutions
24
 * Suite #1A
25
 * 2328 Government Street
26
 * Victoria BC  V8T 5G5
27
 * Canada
28
 *
29
 * (250)385-6040
30
 * www.vividsolutions.com
31
 */
32
package com.vividsolutions.jump.util;
33

    
34
import java.util.ArrayList;
35
import java.util.Collection;
36
import java.util.Collections;
37
import java.util.Iterator;
38
import java.util.List;
39
import java.util.ListIterator;
40

    
41
/**
42
 * Can't add, replace, or remove the first element in the list.
43
 */
44
public class ImmutableFirstElementList implements List {
45

    
46
        private List list = new ArrayList();
47
        
48
        public ImmutableFirstElementList(Object firstElement) {
49
                list.add(firstElement);
50
        }
51

    
52
    public int size() {
53
        return list.size();
54
    }
55

    
56
    public boolean isEmpty() {
57
        return list.isEmpty();
58
    }
59

    
60
    public boolean contains(Object o) {
61
        return list.contains(o);
62
    }
63

    
64
    public Iterator iterator() {
65
            //Prevent Iterator#remove. [Jon Aquino]
66
        return Collections.unmodifiableList(list).iterator();
67
    }
68

    
69
    public Object[] toArray() {
70
        return list.toArray();
71
    }
72

    
73
    public Object[] toArray(Object[] a) {
74
        return list.toArray(a);
75
    }
76

    
77
    public boolean add(Object o) {
78
        return list.add(o);
79
    }
80

    
81
    public boolean remove(Object o) {
82
            //An element equal to the first element may exist later in the list. [Jon Aquino] 
83
                return list.subList(1, list.size()).remove(o);
84
    }
85

    
86
    public boolean containsAll(Collection c) {
87
        return list.containsAll(c);
88
    }
89

    
90
    public boolean addAll(Collection c) {
91
        return list.addAll(c);
92
    }
93

    
94
    public boolean addAll(int index, Collection c) {
95
                return list.addAll(index == 0 ? 1 : index, c);
96
    }
97

    
98
    public boolean removeAll(Collection c) {
99
                return list.subList(1, list.size()).remove(c);
100
    }
101

    
102
    public boolean retainAll(Collection c) {
103
                return list.subList(1, list.size()).retainAll(c);
104
    }
105

    
106
    public void clear() {
107
                list.subList(1, list.size()).clear();
108
    }
109

    
110
    public Object get(int index) {
111
        return list.get(index);
112
    }
113

    
114
    public Object set(int index, Object element) {
115
        if (index == 0) {
116
                return get(0);
117
        }
118
        return list.set(index, element);
119
    }
120

    
121
    public void add(int index, Object element) {
122
                list.add(index == 0 ? 1 : index, element);
123
    }
124

    
125
    public Object remove(int index) {
126
        if (index == 0) { return get(0);
127
        }
128
        return list.remove(index);
129
    }
130

    
131
    public int indexOf(Object o) {
132
        return list.indexOf(o);
133
    }
134

    
135
    public int lastIndexOf(Object o) {
136
        return list.lastIndexOf(o);
137
    }
138

    
139
    public ListIterator listIterator() {
140
                //Prevent Iterator#remove. [Jon Aquino]
141
                return Collections.unmodifiableList(list).listIterator();            
142
    }
143

    
144
    public ListIterator listIterator(int index) {
145
                //Prevent Iterator#remove. [Jon Aquino]
146
                return Collections.unmodifiableList(list).listIterator(index);            
147
    }
148

    
149
    public List subList(int fromIndex, int toIndex) {
150
            if (fromIndex > 0) {
151
                    return list.subList(fromIndex, toIndex);
152
            }
153
            //A bit heavy-handed, but we don't want the first element to be
154
            //modified, moved, or removed. Future: allow the other elements
155
            //to be modified, moved, or removed. [Jon Aquino]
156
                return Collections.unmodifiableList(list).subList(fromIndex, toIndex);                        
157
    }
158

    
159
}