1758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov/*
2758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * Copyright (C) 2012 The Android Open Source Project
3758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov *
4758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * Licensed under the Apache License, Version 2.0 (the "License");
5758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * you may not use this file except in compliance with the License.
6758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * You may obtain a copy of the License at
7758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov *
8758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov *      http://www.apache.org/licenses/LICENSE-2.0
9758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov *
10758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * Unless required by applicable law or agreed to in writing, software
11758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * distributed under the License is distributed on an "AS IS" BASIS,
12758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * See the License for the specific language governing permissions and
14758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * limitations under the License.
15758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov */
16758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov
17758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganovpackage com.android.internal.os;
18758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov
19758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov/**
20758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * Helper class for passing more arguments though a message
21758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * and avoiding allocation of a custom class for wrapping the
22758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * arguments. This class maintains a pool of instances and
23758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * it is responsibility of the client to recycle and instance
24758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov * once it is no longer used.
25758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov */
26758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganovpublic final class SomeArgs {
27758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov
28758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    private static final int MAX_POOL_SIZE = 10;
29758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov
30758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    private static SomeArgs sPool;
31758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    private static int sPoolSize;
32758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    private static Object sPoolLock = new Object();
33758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov
34758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    private SomeArgs mNext;
35758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov
36758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    private boolean mInPool;
37758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov
38758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    public Object arg1;
39758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    public Object arg2;
40758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    public Object arg3;
41758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    public Object arg4;
42c4aad01cbbb69c916ef323693e1fd0560b0eccbaDianne Hackborn    public Object arg5;
43758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    public int argi1;
44758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    public int argi2;
45758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    public int argi3;
46758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    public int argi4;
47758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    public int argi5;
48758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    public int argi6;
49758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov
50758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    private SomeArgs() {
51758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        /* do nothing - reduce visibility */
52758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    }
53758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov
54758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    public static SomeArgs obtain() {
55758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        synchronized (sPoolLock) {
56758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov            if (sPoolSize > 0) {
57758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov                SomeArgs args = sPool;
58758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov                sPool = sPool.mNext;
59758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov                args.mNext = null;
60758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov                args.mInPool = false;
61758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov                sPoolSize--;
62758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov                return args;
63758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov            } else {
64758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov                return new SomeArgs();
65758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov            }
66758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        }
67758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    }
68758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov
69758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    public void recycle() {
70758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        if (mInPool) {
71758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov            throw new IllegalStateException("Already recycled.");
72758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        }
73758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        synchronized (sPoolLock) {
74758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov            clear();
75758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov            if (sPoolSize < MAX_POOL_SIZE) {
76758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov                mNext = sPool;
77758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov                mInPool = true;
78758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov                sPool = this;
79758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov                sPoolSize++;
80758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov            }
81758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        }
82758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    }
83758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov
84758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    private void clear() {
85758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        arg1 = null;
86758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        arg2 = null;
87758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        arg3 = null;
88758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        arg4 = null;
89c4aad01cbbb69c916ef323693e1fd0560b0eccbaDianne Hackborn        arg5 = null;
90758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        argi1 = 0;
91758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        argi2 = 0;
92758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        argi3 = 0;
93758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        argi4 = 0;
94758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        argi5 = 0;
95758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov        argi6 = 0;
96758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov    }
97758143ecfedbe08cc6c4fed0ad8ad7a854194ca4Svetoslav Ganov}
98