Home | History | Annotate | Download | only in os
      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 int argi1;
     43     public int argi2;
     44     public int argi3;
     45     public int argi4;
     46     public int argi5;
     47     public int argi6;
     48 
     49     private SomeArgs() {
     50         /* do nothing - reduce visibility */
     51     }
     52 
     53     public static SomeArgs obtain() {
     54         synchronized (sPoolLock) {
     55             if (sPoolSize > 0) {
     56                 SomeArgs args = sPool;
     57                 sPool = sPool.mNext;
     58                 args.mNext = null;
     59                 args.mInPool = false;
     60                 sPoolSize--;
     61                 return args;
     62             } else {
     63                 return new SomeArgs();
     64             }
     65         }
     66     }
     67 
     68     public void recycle() {
     69         if (mInPool) {
     70             throw new IllegalStateException("Already recycled.");
     71         }
     72         synchronized (sPoolLock) {
     73             clear();
     74             if (sPoolSize < MAX_POOL_SIZE) {
     75                 mNext = sPool;
     76                 mInPool = true;
     77                 sPool = this;
     78                 sPoolSize++;
     79             }
     80         }
     81     }
     82 
     83     private void clear() {
     84         arg1 = null;
     85         arg2 = null;
     86         arg3 = null;
     87         arg4 = null;
     88         argi1 = 0;
     89         argi2 = 0;
     90         argi3 = 0;
     91         argi4 = 0;
     92         argi5 = 0;
     93         argi6 = 0;
     94     }
     95 }
     96