1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.internal.os; 18 19 /** 20 * Helper class for passing more arguments though a message 21 * and avoiding allocation of a custom class for wrapping the 22 * arguments. This class maintains a pool of instances and 23 * it is responsibility of the client to recycle and instance 24 * once it is no longer used. 25 */ 26 public final class SomeArgs { 27 28 private static final int MAX_POOL_SIZE = 10; 29 30 private static SomeArgs sPool; 31 private static int sPoolSize; 32 private static Object sPoolLock = new Object(); 33 34 private SomeArgs mNext; 35 36 private boolean mInPool; 37 38 public Object arg1; 39 public Object arg2; 40 public Object arg3; 41 public Object arg4; 42 public Object arg5; 43 public int argi1; 44 public int argi2; 45 public int argi3; 46 public int argi4; 47 public int argi5; 48 public int argi6; 49 50 private SomeArgs() { 51 /* do nothing - reduce visibility */ 52 } 53 54 public static SomeArgs obtain() { 55 synchronized (sPoolLock) { 56 if (sPoolSize > 0) { 57 SomeArgs args = sPool; 58 sPool = sPool.mNext; 59 args.mNext = null; 60 args.mInPool = false; 61 sPoolSize--; 62 return args; 63 } else { 64 return new SomeArgs(); 65 } 66 } 67 } 68 69 public void recycle() { 70 if (mInPool) { 71 throw new IllegalStateException("Already recycled."); 72 } 73 synchronized (sPoolLock) { 74 clear(); 75 if (sPoolSize < MAX_POOL_SIZE) { 76 mNext = sPool; 77 mInPool = true; 78 sPool = this; 79 sPoolSize++; 80 } 81 } 82 } 83 84 private void clear() { 85 arg1 = null; 86 arg2 = null; 87 arg3 = null; 88 arg4 = null; 89 arg5 = null; 90 argi1 = 0; 91 argi2 = 0; 92 argi3 = 0; 93 argi4 = 0; 94 argi5 = 0; 95 argi6 = 0; 96 } 97 } 98