Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 70f23146c2ec49ac574fe52261c9e5c6b5fb471b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 *      Copyright (C) 2010 10gen Inc.
 *  
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.mongodb.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

public class StringRangeSet implements Set<String> {

    private final int size;
    
    private final static int NUMSTR_LEN = 100;
    private final static String[] NUMSTRS = new String[100];
    static {
        for (int i = 0; i < NUMSTR_LEN; ++i)
            NUMSTRS[i] = String.valueOf(i);
    }

    public StringRangeSet(int size) {
        this.size = size;
    }

    public int size() {
        return size;
    }

    public Iterator<String> iterator() {
        return new Iterator<String>() {

            int index = 0;

            public boolean hasNext() {
                return index < size;
            }

            public String next() {
                if (index < NUMSTR_LEN)
                    return NUMSTRS[index++];
                return String.valueOf(index++);
            }
            
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

    public boolean add(String e) {
        throw new UnsupportedOperationException();
    }

    public boolean addAll(Collection<? extends String> c) {
        throw new UnsupportedOperationException();
    }

    public void clear() {
        throw new UnsupportedOperationException();
    }

    public boolean contains(Object o) {
        int t = Integer.valueOf(String.valueOf(o));
        return t >= 0 && t < size;
    }

    public boolean containsAll(Collection<?> c) {
        for (Object o : c) {
            if (!contains(o)) {
                return false;
            }
        }
        return true;
    }

    public boolean isEmpty() {
        return false;
    }

    public boolean remove(Object o) {
        throw new UnsupportedOperationException();
    }

    public boolean removeAll(Collection<?> c) {
        throw new UnsupportedOperationException();
    }

    public boolean retainAll(Collection<?> c) {
        throw new UnsupportedOperationException();
    }

    public Object[] toArray() {
        String[] array = new String[size()];
        for (int i = 0; i < size; ++i) {
            if (i < NUMSTR_LEN) {
                array[i] = NUMSTRS[i];
            } else {
                array[i] = String.valueOf(i);
            }
        }
        return array;
    }

    public <T> T[] toArray(T[] a) {
        throw new UnsupportedOperationException();
    }
}

Back to the top