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 Object arg7;
50     public Object arg8;
51     public Object arg9;
52     public int argi1;
53     public int argi2;
54     public int argi3;
55     public int argi4;
56     public int argi5;
57     public int argi6;
58 
SomeArgs()59     private SomeArgs() {
60         /* do nothing - reduce visibility */
61     }
62 
obtain()63     public static SomeArgs obtain() {
64         synchronized (sPoolLock) {
65             if (sPoolSize > 0) {
66                 SomeArgs args = sPool;
67                 sPool = sPool.mNext;
68                 args.mNext = null;
69                 args.mInPool = false;
70                 sPoolSize--;
71                 return args;
72             } else {
73                 return new SomeArgs();
74             }
75         }
76     }
77 
complete()78     public void complete() {
79         synchronized (this) {
80             if (mWaitState != WAIT_WAITING) {
81                 throw new IllegalStateException("Not waiting");
82             }
83             mWaitState = WAIT_FINISHED;
84             notifyAll();
85         }
86     }
87 
recycle()88     public void recycle() {
89         if (mInPool) {
90             throw new IllegalStateException("Already recycled.");
91         }
92         if (mWaitState != WAIT_NONE) {
93             return;
94         }
95         synchronized (sPoolLock) {
96             clear();
97             if (sPoolSize < MAX_POOL_SIZE) {
98                 mNext = sPool;
99                 mInPool = true;
100                 sPool = this;
101                 sPoolSize++;
102             }
103         }
104     }
105 
clear()106     private void clear() {
107         arg1 = null;
108         arg2 = null;
109         arg3 = null;
110         arg4 = null;
111         arg5 = null;
112         arg6 = null;
113         arg7 = null;
114         argi1 = 0;
115         argi2 = 0;
116         argi3 = 0;
117         argi4 = 0;
118         argi5 = 0;
119         argi6 = 0;
120     }
121 }
122