blob: 7edf4cc0aa187e6af40d15f37708d6859ac8fb43 [file] [log] [blame]
Svetoslav Ganov758143e2012-08-06 16:40:27 -07001/*
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
17package 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 */
26public 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
Dianne Hackborn91097de2014-04-04 18:02:06 -070038 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
Svetoslav Ganov758143e2012-08-06 16:40:27 -070043 public Object arg1;
44 public Object arg2;
45 public Object arg3;
46 public Object arg4;
Dianne Hackbornc4aad012013-02-22 15:05:25 -080047 public Object arg5;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070048 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 recycle() {
75 if (mInPool) {
76 throw new IllegalStateException("Already recycled.");
77 }
Dianne Hackborn91097de2014-04-04 18:02:06 -070078 if (mWaitState != WAIT_NONE) {
79 return;
80 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -070081 synchronized (sPoolLock) {
82 clear();
83 if (sPoolSize < MAX_POOL_SIZE) {
84 mNext = sPool;
85 mInPool = true;
86 sPool = this;
87 sPoolSize++;
88 }
89 }
90 }
91
92 private void clear() {
93 arg1 = null;
94 arg2 = null;
95 arg3 = null;
96 arg4 = null;
Dianne Hackbornc4aad012013-02-22 15:05:25 -080097 arg5 = null;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070098 argi1 = 0;
99 argi2 = 0;
100 argi3 = 0;
101 argi4 = 0;
102 argi5 = 0;
103 argi6 = 0;
104 }
105}