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.recyclable.impl;
33
34 import java.lang.ref.Reference;
35 import java.util.Arrays;
36 import java.util.concurrent.BlockingQueue;
37 import java.util.concurrent.LinkedBlockingQueue;
38 import org.spf4j.base.IntMath;
39 import org.spf4j.base.ReferenceType;
40 import org.spf4j.recyclable.SizedRecyclingSupplier;
41
42 /**
43 * recycling supplier that allows you to recycle objects.
44 * Recycling objects is dangerous business, care should be used.
45 * @author zoly
46 */
47 public final class Powerof2SizedGlobalRecyclingSupplier<T> implements SizedRecyclingSupplier<T> {
48
49 private final SizedRecyclingSupplier.Factory<T> factory;
50
51 private final ReferenceType refType;
52
53 private final BlockingQueue<Reference<T>>[] objects;
54
55 public Powerof2SizedGlobalRecyclingSupplier(final Factory<T> factory, final ReferenceType refType) {
56 this.factory = factory;
57 this.refType = refType;
58 objects = new BlockingQueue[28];
59 for (int i = 0; i < objects.length; i++) {
60 objects[i] = new LinkedBlockingQueue<>();
61 }
62 }
63
64 @Override
65 public T get(final int size) {
66 int idx = IntMath.closestPowerOf2(size);
67 BlockingQueue<Reference<T>> refs = objects[idx];
68 Reference<T> ref;
69 do {
70 ref = refs.poll();
71 if (ref == null) {
72 int actualSize = 1 << idx;
73 return factory.create(actualSize);
74 } else {
75 T result = ref.get();
76 if (result != null) {
77 return result;
78 }
79 }
80 } while (true);
81 }
82
83 @Override
84 public void recycle(final T object) {
85 int size = factory.size(object);
86 int idx = IntMath.closestPowerOf2(size);
87 BlockingQueue<Reference<T>> refs = objects[idx];
88 refs.add(refType.create(object));
89 }
90
91 @Override
92 public String toString() {
93 return "Powerof2SizedGlobalRecyclingSupplier{" + "factory=" + factory + ", refType="
94 + refType + ", objects=" + Arrays.toString(objects) + '}';
95 }
96
97 }