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