1 /*
2 * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Additionally licensed with:
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS,
28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 */
32 package org.spf4j.concurrent;
33
34 import java.util.concurrent.atomic.AtomicLong;
35
36 /**
37 *
38 * @author zoly
39 */
40 public final class ScalableSequence implements Sequence {
41
42 private final AtomicLong counter;
43
44 private final long bucketSize;
45
46 private final ThreadLocal<Bucket> buckets;
47
48 private static final class Bucket {
49
50 private long current;
51 private long max;
52
53 Bucket(final long start, final long end) {
54 this.current = start;
55 this.max = end;
56 }
57
58 public void reset(final long start, final long end) {
59 this.current = start;
60 this.max = end;
61 }
62
63 public boolean hasValue() {
64 return current < max;
65 }
66
67 public long getValue() {
68 return current++;
69 }
70
71 }
72
73 public ScalableSequence(final long start, final int bucketSize) {
74 if (bucketSize < 10) {
75 throw new IllegalArgumentException("Bucket size must be greater than 10 and not " + bucketSize);
76 }
77 this.counter = new AtomicLong(start);
78 this.bucketSize = bucketSize;
79 this.buckets = new ThreadLocal() {
80 @Override
81 protected Bucket initialValue() {
82 long start = counter.getAndAdd(bucketSize);
83 return new Bucket(start, start + bucketSize);
84 }
85 };
86 }
87
88 @Override
89 public long next() {
90 Bucket bucket = buckets.get();
91 if (bucket.hasValue()) {
92 return bucket.getValue();
93 } else {
94 long start = counter.getAndAdd(bucketSize);
95 bucket.reset(start + 1, start + bucketSize);
96 return start;
97 }
98 }
99
100 @Override
101 public String toString() {
102 return "ScalableSequence{" + "counter=" + counter + ", bucketSize=" + bucketSize + '}';
103 }
104
105 }