11/**
22 The MIT License (MIT)
33
4- Copyright (c) 2010-2019 head systems, ltd
4+ Copyright (c) 2010-2020 head systems, ltd
55
66 Permission is hereby granted, free of charge, to any person obtaining a copy of
77 this software and associated documentation files (the "Software"), to deal in
@@ -119,28 +119,42 @@ public synchronized void remove (IndexElementKey key, Object o) {
119119 removeObjects (key , o );
120120 }
121121
122- public synchronized List <Object > getObjectsByKey (final IndexElementKey key ) {
122+ public synchronized List <Object > getObjectsByKey (final IndexElementKey key , final int amount ) {
123123 final ArrayList <Object > r = new ArrayList <Object >();
124124 boolean cnue = true ;
125125 ArrayList <IndexElementList > targets = new ArrayList <IndexElementList >();
126126 IndexElementList el = list .get (start );
127127 //el.sort();
128128 targets .add (el );
129+ int amount_ = 0 ;
129130 while (cnue ) {
130131 final ArrayList <IndexElementList > ntargets = new ArrayList <IndexElementList >();
131132 for (IndexElementList target : targets ) {
132133 //target.sort();
133- if (target .getType ()==1 ) { //leaf
134- r .addAll (target .getObjectsByKey (key ));
134+ if (target .getType () == 1 ) { //leaf
135+ if (amount == 0 ) {
136+ final List <Object > list = target .getObjectsByKey (key );
137+ r .addAll (list );
138+ } else {
139+ if (amount - amount_ > 0 ) {
140+ final List <Object > list = target .getObjectsByKey (key );
141+ for (Object o : list ) {
142+ if (amount - amount_ > 0 ) {
143+ r .add (o );
144+ }
145+ amount_ ++;
146+ }
147+ }
148+ }
135149 cnue = false ;
136150 } else {
137151 final ArrayList <Integer > cptr = target .getChildElementsPtr (key );
138- if (cptr .size ()> 0 ) {
152+ if (cptr .size () > 0 ) {
139153 for (Integer i : cptr ) {
140154 ntargets .add (list .get (i ));
141155 }
142156 }
143- if (target .getLc ()> 0 ) {
157+ if (target .getLc () > 0 ) {
144158 ntargets .add (list .get (target .getLc ())); //get by last child
145159 }
146160 }
@@ -151,24 +165,38 @@ public synchronized List<Object> getObjectsByKey (final IndexElementKey key) {
151165 }
152166
153167 public synchronized List <Object > getContent () {
154- ArrayList <Object > res = new ArrayList <Object >();
155- ArrayList <IndexElementList > levelNodes = new ArrayList <IndexElementList >();
168+ return getContent (0 );
169+ }
170+
171+ public synchronized List <Object > getContent (int amount ) {
172+ ArrayList <Object > res = new ArrayList <>();
173+ ArrayList <IndexElementList > levelNodes = new ArrayList <>();
156174 boolean cnue = true ;
157175 IndexElementList el = list .get (start );
158176 el .sort ();
159177 levelNodes .add (el );
178+ int amount_ = 0 ;
160179 while (cnue ) {
161- ArrayList <IndexElementList > inNodes = new ArrayList <IndexElementList >();
180+ ArrayList <IndexElementList > inNodes = new ArrayList <>();
162181 int lc = 0 ;
163182 for (int k =0 ; k <levelNodes .size (); k ++) {
164183 levelNodes .get (k ).sort ();
165- if (levelNodes .get (k ).getType ()==1 ) {
166- cnue = false ;
167- for (IndexElement ie : levelNodes .get (k ).getElementList ()) {
168- if (levelNodes .get (k ).getType ()==1 ) {
184+ if (levelNodes .get (k ).getType () == 1 ) {
185+ if (amount == 0 ) {
186+ for (IndexElement ie : levelNodes .get (k ).getElementList ()) {
169187 res .add (ie .getElement ());
170188 }
189+ } else {
190+ if (amount - amount_ > 0 ) {
191+ for (IndexElement ie : levelNodes .get (k ).getElementList ()) {
192+ if (amount - amount_ > 0 ) {
193+ res .add (ie .getElement ());
194+ }
195+ amount_ ++;
196+ }
197+ }
171198 }
199+ cnue = false ;
172200 } else {
173201 for (int i =0 ; i <levelNodes .get (k ).getElementList ().size (); i ++) {
174202 inNodes .add (this .list .get ((Integer )levelNodes .get (k ).getElementList ().get (i ).getElement ()));
@@ -302,11 +330,23 @@ public synchronized Object getObjectByKey (final IndexElementKey key) {
302330
303331
304332 public synchronized List <Object > getObjectsByKey (final int obj ) {
305- return getObjectsByKey (new IndexElementKey (new Integer []{obj }));
333+ return getObjectsByKey (new IndexElementKey (new Integer []{obj }), 0 );
306334 }
307335
308336 public synchronized List <Object > getObjectsByKey (final long obj ) {
309- return getObjectsByKey (new IndexElementKey (new Long []{obj }));
337+ return getObjectsByKey (new IndexElementKey (new Long []{obj }), 0 );
338+ }
339+
340+ public synchronized List <Object > getObjectsByKey (final String obj ) {
341+ return getObjectsByKey (new IndexElementKey (new String []{obj }), 0 );
342+ }
343+
344+ public synchronized List <Object > getObjectsByKey (final int obj , final int amount ) {
345+ return getObjectsByKey (new IndexElementKey (new Integer []{obj }), amount );
346+ }
347+
348+ public synchronized List <Object > getObjectsByKey (final long obj , final int amount ) {
349+ return getObjectsByKey (new IndexElementKey (new Long []{obj }), amount );
310350 }
311351
312352 //for non-unique indexes
0 commit comments