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