Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2018 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 android.telephony.embms.cts;
     18 
     19 /**
     20  * Copied from com.android.internal.os to avoid linking errors.
     21  */
     22 public final class SomeArgs {
     23 
     24     private static final int MAX_POOL_SIZE = 10;
     25 
     26     private static SomeArgs sPool;
     27     private static int sPoolSize;
     28     private static Object sPoolLock = new Object();
     29 
     30     private SomeArgs mNext;
     31 
     32     private boolean mInPool;
     33 
     34     static final int WAIT_NONE = 0;
     35     static final int WAIT_WAITING = 1;
     36     static final int WAIT_FINISHED = 2;
     37     int mWaitState = WAIT_NONE;
     38 
     39     public Object arg1;
     40     public Object arg2;
     41     public Object arg3;
     42     public Object arg4;
     43     public Object arg5;
     44     public Object arg6;
     45     public Object arg7;
     46     public Object arg8;
     47     public Object arg9;
     48     public int argi1;
     49     public int argi2;
     50     public int argi3;
     51     public int argi4;
     52     public int argi5;
     53     public int argi6;
     54 
     55     private SomeArgs() {
     56         /* do nothing - reduce visibility */
     57     }
     58 
     59     public static SomeArgs obtain() {
     60         synchronized (sPoolLock) {
     61             if (sPoolSize > 0) {
     62                 SomeArgs args = sPool;
     63                 sPool = sPool.mNext;
     64                 args.mNext = null;
     65                 args.mInPool = false;
     66                 sPoolSize--;
     67                 return args;
     68             } else {
     69                 return new SomeArgs();
     70             }
     71         }
     72     }
     73 
     74     public void complete() {
     75         synchronized (this) {
     76             if (mWaitState != WAIT_WAITING) {
     77                 throw new IllegalStateException("Not waiting");
     78             }
     79             mWaitState = WAIT_FINISHED;
     80             notifyAll();
     81         }
     82     }
     83 
     84     public void recycle() {
     85         if (mInPool) {
     86             throw new IllegalStateException("Already recycled.");
     87         }
     88         if (mWaitState != WAIT_NONE) {
     89             return;
     90         }
     91         synchronized (sPoolLock) {
     92             clear();
     93             if (sPoolSize < MAX_POOL_SIZE) {
     94                 mNext = sPool;
     95                 mInPool = true;
     96                 sPool = this;
     97                 sPoolSize++;
     98             }
     99         }
    100     }
    101 
    102     private void clear() {
    103         arg1 = null;
    104         arg2 = null;
    105         arg3 = null;
    106         arg4 = null;
    107         arg5 = null;
    108         arg6 = null;
    109         arg7 = null;
    110         arg8 = null;
    111         arg9 = null;
    112         argi1 = 0;
    113         argi2 = 0;
    114         argi3 = 0;
    115         argi4 = 0;
    116         argi5 = 0;
    117         argi6 = 0;
    118     }
    119 }
    120