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     static final int WAIT_NONE = 0;
     39     static final int WAIT_WAITING = 1;
     40     static final int WAIT_FINISHED = 2;
     41     int mWaitState = WAIT_NONE;
     42 
     43     public Object arg1;
     44     public Object arg2;
     45     public Object arg3;
     46     public Object arg4;
     47     public Object arg5;
     48     public Object arg6;
     49     public int argi1;
     50     public int argi2;
     51     public int argi3;
     52     public int argi4;
     53     public int argi5;
     54     public int argi6;
     55 
     56     private SomeArgs() {
     57         /* do nothing - reduce visibility */
     58     }
     59 
     60     public static SomeArgs obtain() {
     61         synchronized (sPoolLock) {
     62             if (sPoolSize > 0) {
     63                 SomeArgs args = sPool;
     64                 sPool = sPool.mNext;
     65                 args.mNext = null;
     66                 args.mInPool = false;
     67                 sPoolSize--;
     68                 return args;
     69             } else {
     70                 return new SomeArgs();
     71             }
     72         }
     73     }
     74 
     75     public void recycle() {
     76         if (mInPool) {
     77             throw new IllegalStateException("Already recycled.");
     78         }
     79         if (mWaitState != WAIT_NONE) {
     80             return;
     81         }
     82         synchronized (sPoolLock) {
     83             clear();
     84             if (sPoolSize < MAX_POOL_SIZE) {
     85                 mNext = sPool;
     86                 mInPool = true;
     87                 sPool = this;
     88                 sPoolSize++;
     89             }
     90         }
     91     }
     92 
     93     private void clear() {
     94         arg1 = null;
     95         arg2 = null;
     96         arg3 = null;
     97         arg4 = null;
     98         arg5 = null;
     99         arg6 = null;
    100         argi1 = 0;
    101         argi2 = 0;
    102         argi3 = 0;
    103         argi4 = 0;
    104         argi5 = 0;
    105         argi6 = 0;
    106     }
    107 }
    108