View Javadoc
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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35  import org.spf4j.base.ReferenceType;
36  import org.spf4j.recyclable.SizedRecyclingSupplier;
37  
38  /**
39   *
40   * @author zoly
41   */
42  public final class ArraySuppliers {
43  
44    private ArraySuppliers() {
45    }
46  
47    public static final class Objects {
48  
49      public static final SizedRecyclingSupplier<Object[]> TL_SUPPLIER
50              = new Powerof2ThreadLocalRecyclingSupplier<>(new SizedRecyclingSupplier.Factory<Object[]>() {
51  
52        @Override
53        @SuppressFBWarnings("SUA_SUSPICIOUS_UNINITIALIZED_ARRAY")
54        public Object[] create(final int size) {
55          return new Object[size];
56        }
57  
58        @Override
59        public int size(final Object[] object) {
60          return object.length;
61        }
62      }, ReferenceType.SOFT);
63  
64      private Objects() {
65      }
66  
67    }
68  
69    public static final class Bytes {
70  
71      private static final SizedRecyclingSupplier.Factory<byte[]> FACTORY
72              = new SizedRecyclingSupplier.Factory<byte[]>() {
73  
74        @Override
75        @SuppressFBWarnings("SUA_SUSPICIOUS_UNINITIALIZED_ARRAY")
76        public byte[] create(final int size) {
77          return new byte[size];
78        }
79  
80        @Override
81        public int size(final byte[] object) {
82          return object.length;
83        }
84      };
85  
86      public static final SizedRecyclingSupplier<byte[]> TL_SUPPLIER
87              = new Powerof2ThreadLocalRecyclingSupplier<>(FACTORY, ReferenceType.SOFT);
88  
89      public static final SizedRecyclingSupplier<byte[]> GL_SUPPLIER
90              = new Powerof2SizedGlobalRecyclingSupplier<>(FACTORY, ReferenceType.SOFT);
91  
92      public static final SizedRecyclingSupplier<byte[]> JAVA_NEW
93              = new SizedRecyclingSupplier<byte[]>() {
94        @Override
95        @SuppressFBWarnings("SUA_SUSPICIOUS_UNINITIALIZED_ARRAY")
96        public byte[] get(final int size) {
97          return new byte[size];
98        }
99  
100       @Override
101       public void recycle(final byte[] object) {
102         // Let the GC deal with this
103       }
104     };
105 
106     private Bytes() {
107     }
108 
109   }
110 
111   public static final class Chars {
112 
113     private static final SizedRecyclingSupplier.Factory<char[]> FACTORY
114             = new SizedRecyclingSupplier.Factory<char[]>() {
115 
116       @Override
117       @SuppressFBWarnings("SUA_SUSPICIOUS_UNINITIALIZED_ARRAY")
118       public char[] create(final int size) {
119         return new char[size];
120       }
121 
122       @Override
123       public int size(final char[] object) {
124         return object.length;
125       }
126     };
127 
128     public static final SizedRecyclingSupplier<char[]> TL_SUPPLIER
129             = new Powerof2ThreadLocalRecyclingSupplier<>(FACTORY, ReferenceType.SOFT);
130 
131     public static final SizedRecyclingSupplier<char[]> GL_SUPPLIER
132             = new Powerof2SizedGlobalRecyclingSupplier<>(FACTORY, ReferenceType.SOFT);
133 
134     public static final SizedRecyclingSupplier<char[]> JAVA_NEW
135             = new SizedRecyclingSupplier<char[]>() {
136 
137       @Override
138       @SuppressFBWarnings("SUA_SUSPICIOUS_UNINITIALIZED_ARRAY")
139       public char[] get(final int size) {
140         return new char[size];
141       }
142 
143       @Override
144       public void recycle(final char[] object) {
145         // Let the GC deal with this
146       }
147     };
148 
149     private Chars() {
150     }
151 
152   }
153 
154 }