1+ /**
2+ * Copyright (C) 2010 10gen Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+ package com .mongodb .util ;
17+
18+ import java .util .Collection ;
19+ import java .util .Iterator ;
20+ import java .util .Set ;
21+
22+ public class StringRangeSet implements Set <String > {
23+ private final int size ;
24+ public StringRangeSet (int size ){
25+ this .size = size ;
26+ }
27+ public int size (){
28+ return size ;
29+ }
30+
31+ public Iterator <String > iterator (){
32+ return new Iterator <String >(){
33+ int index = 0 ;
34+ @ Override
35+ public boolean hasNext () {
36+ return index < size ;
37+ }
38+
39+ @ Override
40+ public String next () {
41+ return String .valueOf (index ++);
42+ }
43+
44+ @ Override
45+ public void remove () {
46+ throw new UnsupportedOperationException ();
47+ }
48+
49+ };
50+ }
51+ @ Override
52+ public boolean add (String e ) {
53+ throw new UnsupportedOperationException ();
54+ }
55+ @ Override
56+ public boolean addAll (Collection <? extends String > c ) {
57+ throw new UnsupportedOperationException ();
58+ }
59+ @ Override
60+ public void clear () {
61+ throw new UnsupportedOperationException ();
62+ }
63+
64+ @ Override
65+ public boolean contains (Object o ) {
66+ int t = Integer .valueOf (String .valueOf (o ));
67+ return t >= 0 && t < size ;
68+ }
69+ @ Override
70+ public boolean containsAll (Collection <?> c ) {
71+ for (Object o : c ) {
72+ if (!contains (o )){
73+ return false ;
74+ }
75+ }
76+ return true ;
77+ }
78+ @ Override
79+ public boolean isEmpty () {
80+ return false ;
81+ }
82+ @ Override
83+ public boolean remove (Object o ) {
84+ throw new UnsupportedOperationException ();
85+ }
86+ @ Override
87+ public boolean removeAll (Collection <?> c ) {
88+ throw new UnsupportedOperationException ();
89+ }
90+ @ Override
91+ public boolean retainAll (Collection <?> c ) {
92+ throw new UnsupportedOperationException ();
93+ }
94+ @ Override
95+ public Object [] toArray () {
96+ String [] array = new String [size ()];
97+ for (int i = 0 ; i < size ; i ++){
98+ array [i ] = String .valueOf (i +1 );
99+ }
100+ return array ;
101+ }
102+ @ Override
103+ public <T > T [] toArray (T [] a ) {
104+ throw new UnsupportedOperationException ();
105+ }
106+
107+ }
0 commit comments