1717
1818import com .couchbase .client .java .Bucket ;
1919import com .couchbase .client .java .datastructures .collections .CouchbaseArrayList ;
20+ import com .couchbase .client .java .document .json .JsonObject ;
2021
2122import java .util .Collection ;
2223import java .util .Iterator ;
2627import java .util .Spliterator ;
2728import java .util .stream .StreamSupport ;
2829
30+ import static java .lang .Boolean .TRUE ;
2931import static java .util .Objects .requireNonNull ;
3032import static java .util .Spliterators .spliteratorUnknownSize ;
3133import static java .util .stream .Collectors .toList ;
34+ import static org .jnosql .diana .couchbase .key .DefaultCouchbaseBucketManagerFactory .LIST ;
3235
3336/**
3437 * The couchbase implementation to {@link List}
3841 *
3942 * @param <T> the object to be stored.
4043 */
41- public class CouchbaseList <T > extends CouchbaseCollection <T > implements List <T > {
44+ class CouchbaseList <T > extends CouchbaseCollection <T > implements List <T > {
4245
46+ private static final int NOT_FOUND = -1 ;
4347 private final String bucketName ;
44- private final CouchbaseArrayList <String > arrayList ;
48+ private final CouchbaseArrayList <JsonObject > arrayList ;
4549
4650 CouchbaseList (Bucket bucket , String bucketName , Class <T > clazz ) {
4751 super (clazz );
48- this .bucketName = bucketName + ":list" ;
52+ this .bucketName = bucketName + LIST ;
4953 this .arrayList = new CouchbaseArrayList (this .bucketName , bucket );
5054 }
5155
@@ -63,7 +67,7 @@ public boolean isEmpty() {
6367 @ Override
6468 public boolean add (T t ) {
6569 requireNonNull (t , "object is required" );
66- return arrayList .add (JSONB .toJson (t ));
70+ return arrayList .add (JsonObjectCouchbaseUtil .toJson (JSONB , t ));
6771 }
6872
6973 @ Override
@@ -82,25 +86,26 @@ public void clear() {
8286
8387 @ Override
8488 public T get (int i ) {
85- return JSONB .fromJson (arrayList .get (i ), clazz );
89+ JsonObject jsonObject = arrayList .get (i );
90+ return JSONB .fromJson (jsonObject .toString (), clazz );
8691 }
8792
8893 @ Override
8994 public T set (int i , T t ) {
9095 requireNonNull (t , "object is required" );
91- String json = arrayList .set (i , JSONB .toJson (t ));
96+ JsonObject json = arrayList .set (i , JsonObjectCouchbaseUtil .toJson (JSONB , t ));
9297 if (Objects .nonNull (json )) {
93- return JSONB .fromJson (json , clazz );
98+ return JSONB .fromJson (json . toString () , clazz );
9499 }
95100 return null ;
96101 }
97102
98103
99104 @ Override
100105 public T remove (int i ) {
101- String json = arrayList .remove (i );
106+ JsonObject json = arrayList .remove (i );
102107 if (Objects .nonNull (json )) {
103- return JSONB .fromJson (json , clazz );
108+ return JSONB .fromJson (json . toString () , clazz );
104109 }
105110 return null ;
106111 }
@@ -109,99 +114,128 @@ public T remove(int i) {
109114 @ Override
110115 public Iterator <T > iterator () {
111116 return StreamSupport .stream (arrayList .spliterator (), false )
112- .map (fromJSON ( ))
117+ .map (s -> JSONB . fromJson ( s . toString (), clazz ))
113118 .collect (toList ()).iterator ();
114119 }
115120
116121 @ Override
117122 public Object [] toArray () {
118123 return StreamSupport .stream (arrayList .spliterator (), false )
119- .map (fromJSON ( ))
124+ .map (s -> JSONB . fromJson ( s . toString (), clazz ))
120125 .toArray (Object []::new );
121126 }
122127
123128 @ Override
124129 public <T1 > T1 [] toArray (T1 [] t1s ) {
125130 requireNonNull (t1s , "arrys is required" );
126131 return StreamSupport .stream (arrayList .spliterator (), false )
127- .map (fromJSON ( ))
132+ .map (s -> JSONB . fromJson ( s . toString (), clazz ))
128133 .toArray (size -> t1s );
129134 }
130135
131136 @ Override
132137 public boolean retainAll (Collection <?> collection ) {
133138 requireNonNull (collection , "collection is required" );
134- return arrayList .retainAll (collection .stream ().map (JSONB ::toJson ).collect (toList ()));
139+ collection .removeIf (e -> indexOf (e ) == NOT_FOUND );
140+ return true ;
135141 }
136142
137143 @ Override
138144 public boolean removeAll (Collection <?> collection ) {
139145 requireNonNull (collection , "collection is required" );
140- return arrayList .removeAll (collection .stream ().map (JSONB ::toJson ).collect (toList ()));
146+
147+ boolean removeAll = true ;
148+ for (Object object : collection ) {
149+ if (!this .remove (object )) {
150+ removeAll = false ;
151+ }
152+ }
153+ return removeAll ;
141154 }
142155
143156 @ Override
144157 public void add (int i , T t ) {
145158 requireNonNull (t , "object is required" );
146- arrayList .add (i , JSONB .toJson (t ));
159+ arrayList .add (i , JsonObjectCouchbaseUtil .toJson (JSONB , t ));
147160 }
148161
149162 @ Override
150163 public int indexOf (Object o ) {
151164 requireNonNull (o , "object is required" );
152- return arrayList .indexOf (JSONB .toJson (o ));
165+ int index = 0 ;
166+ for (JsonObject jsonObject : arrayList ) {
167+ if (jsonObject .toString ().equals (JsonObjectCouchbaseUtil .toJson (JSONB , o ).toString ())) {
168+ return index ;
169+ }
170+ index ++;
171+ }
172+ return NOT_FOUND ;
153173 }
154174
155175 @ Override
156176 public int lastIndexOf (Object o ) {
157177 requireNonNull (o , "object is required" );
158- return arrayList .lastIndexOf (JSONB .toJson (o ));
178+ for (int index = arrayList .size () - 1 ; index >= 0 ; index --) {
179+ JsonObject jsonObject = arrayList .get (index );
180+ if (jsonObject .toString ().equals (JsonObjectCouchbaseUtil .toJson (JSONB , o ).toString ())) {
181+ return index ;
182+ }
183+ }
184+ return NOT_FOUND ;
159185 }
160186
161187 @ Override
162188 public ListIterator <T > listIterator () {
163189 return StreamSupport .stream (spliteratorUnknownSize (arrayList .listIterator (), Spliterator .ORDERED ),
164- false ).map (fromJSON ( ))
190+ false ).map (s -> JSONB . fromJson ( s . toString (), clazz ))
165191 .collect (toList ())
166192 .listIterator ();
167193 }
168194
169195 @ Override
170196 public ListIterator <T > listIterator (int i ) {
171197 return StreamSupport .stream (spliteratorUnknownSize (arrayList .listIterator (i ), Spliterator .ORDERED ),
172- false ).map (fromJSON ( ))
198+ false ).map (s -> JSONB . fromJson ( s . toString (), clazz ))
173199 .collect (toList ())
174200 .listIterator ();
175201 }
176202
177203 @ Override
178204 public List <T > subList (int i , int i1 ) {
179205 return arrayList .subList (i , i1 ).stream ().
180- map (fromJSON ( ))
206+ map (s -> JSONB . fromJson ( s . toString (), clazz ))
181207 .collect (toList ());
182208 }
183209
184210 @ Override
185211 public boolean remove (Object o ) {
186212 requireNonNull (o , "object is required" );
187- return arrayList .remove (JSONB .toJson (o ));
213+ int index = indexOf (o );
214+ if (index >= 0 ) {
215+ arrayList .remove (index );
216+ return true ;
217+ }
218+ return false ;
188219 }
189220
190221 @ Override
191222 public boolean containsAll (Collection <?> collection ) {
192223 requireNonNull (collection , "collection is required" );
193- return arrayList . containsAll ( collection .stream ().map (JSONB :: toJson ). collect ( toList ()) );
224+ return collection .stream ().map (this :: contains ). allMatch ( TRUE :: equals );
194225 }
195226
196227 @ Override
197228 public boolean contains (Object o ) {
198229 requireNonNull (o , "object is required" );
199- return arrayList . contains ( JSONB . toJson ( o )) ;
230+ return indexOf ( o ) != NOT_FOUND ;
200231 }
201232
202233 @ Override
203234 public boolean addAll (int i , Collection <? extends T > collection ) {
204235 requireNonNull (collection , "collection is required" );
205- return arrayList .addAll (i , collection .stream ().map (JSONB ::toJson ).collect (toList ()));
236+ List <JsonObject > objects = collection .stream ().map (s -> JsonObjectCouchbaseUtil .toJson (JSONB , s )).collect (toList ());
237+ return arrayList .addAll (i , objects );
206238 }
239+
240+
207241}
0 commit comments