Project

General

Profile

Download (8.89 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / main / InternalStackFrame.java @ 30094332

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library.main;
21

    
22
import org.distorted.library.effect.EffectType;
23

    
24
import java.util.HashMap;
25
import java.util.LinkedList;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
public class InternalStackFrame
30
{
31
  private static class Job
32
    {
33
    InternalObject object;
34
    int action;
35

    
36
    Job(InternalObject o, int a)
37
      {
38
      object = o;
39
      action = a;
40
      }
41
    }
42

    
43
  private static LinkedList<InternalObject> mCommonDoneList = new LinkedList<>(); //
44
  private static HashMap<Long,Job> mCommonToDoMap           = new HashMap<>();    // Common
45
  private static long mCommonNextClientID                   = 0;                  // InternalObject
46
  private static long mCommonNextSystemID                   = 0;                  // (postprocessing)
47

    
48
  private LinkedList<InternalObject> mDoneList; //
49
  private HashMap<Long,Job> mToDoMap;           //
50
  private long mNextClientID;                   // InternalObject
51
  private long mNextSystemID;                   //
52
  private long mTaskId;                         //
53

    
54
  private boolean mInitialized;                 // DistortedLibrary
55

    
56
  private int[] mMax;                           // EffectQueue
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  InternalStackFrame(long taskID)
61
    {
62
    mDoneList     = new LinkedList<>();
63
    mToDoMap      = new HashMap<>();
64
    mNextClientID = 0;
65
    mNextSystemID = 0;
66
    mTaskId       = taskID;
67
    mMax          = new int[EffectType.LENGTH];
68

    
69
    EffectType.reset(mMax);
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
  long getTaskId()
75
    {
76
    return mTaskId;
77
    }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  static void onPauseCommon()
82
    {
83
    onPauseGeneric(mCommonDoneList,mCommonToDoMap);
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  void onPause()
89
    {
90
    onPauseGeneric(mDoneList,mToDoMap);
91
    }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
  static void onPauseGeneric(LinkedList<InternalObject> list, HashMap<Long,Job> map)
96
    {
97
    int num = list.size();
98

    
99
    try
100
      {
101
      for (int i=0; i<num; i++)
102
        {
103
        InternalObject object = list.removeFirst();
104
        Job job = new Job(object, InternalObject.JOB_CREATE);
105
        map.put(object.getID(),job);
106
        object.recreate();
107
        }
108
      }
109
    catch( Exception ignored )
110
      {
111
      // something else removed an object in the meantime; ignore
112
      }
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  void toDo()
118
    {
119
    toDoGeneric(mDoneList,mToDoMap);
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  static void toDoCommon()
125
    {
126
    toDoGeneric(mCommonDoneList,mCommonToDoMap);
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
  static void toDoGeneric(LinkedList<InternalObject> list, HashMap<Long,Job> map)
132
    {
133
    for(Long key: map.keySet())
134
      {
135
      Job job = map.get(key);
136
      InternalObject object = job.object;
137

    
138
      if( job.action==InternalObject.JOB_CREATE )
139
        {
140
        object.create();
141
        list.add(object);
142
        }
143
      else if( job.action==InternalObject.JOB_DELETE )
144
        {
145
        object.delete();
146
        }
147
      }
148

    
149
    map.clear();
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  static void cleanCommon()
155
    {
156
    mCommonDoneList.clear();
157
    mCommonToDoMap.clear();
158
    mCommonNextClientID = 0;
159
    mCommonNextSystemID = 0;
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  long generateID(int type, int storage)
165
    {
166
    if( storage==InternalObject.STORAGE_PRIVATE )
167
      {
168
      return type==InternalObject.TYPE_SYST ? --mNextSystemID : ++mNextClientID;
169
      }
170
    else
171
      {
172
      return type==InternalObject.TYPE_SYST ? --mCommonNextSystemID : ++mCommonNextClientID;
173
      }
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  void addToDoneList(InternalObject obj, int storage)
179
    {
180
    if( storage==InternalObject.STORAGE_PRIVATE )
181
      {
182
      if( !mDoneList.contains(obj) )
183
        {
184
        mDoneList.add(obj);
185
        }
186
      }
187
    else
188
      {
189
      if( !mCommonDoneList.contains(obj) ) mCommonDoneList.add(obj);
190
      }
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  void removeFromDoneList(InternalObject obj, int storage)
196
    {
197
    if( storage==InternalObject.STORAGE_PRIVATE )
198
      {
199
      mDoneList.remove(obj);
200
      }
201
    else
202
      {
203
      mCommonDoneList.remove(obj);
204
      }
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  void markFor(InternalObject obj, long id, int storage, int jobType)
210
    {
211
    if( storage==InternalObject.STORAGE_PRIVATE )
212
      {
213
      mDoneList.remove(obj);
214
      mToDoMap.put(id, new Job(obj,jobType) );
215
      }
216
    else
217
      {
218
      mCommonDoneList.remove(obj);
219
      mCommonToDoMap.put(id, new Job(obj,jobType) );
220
      }
221
    }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
  boolean isInitialized()
226
    {
227
    return mInitialized;
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  void setInitialized(boolean init)
233
    {
234
    mInitialized = init;
235
    }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
  int getMax(int index)
240
    {
241
    return mMax[index];
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  boolean setMax(int index, int max)
247
    {
248
    if( !mInitialized || max<mMax[index] )
249
      {
250
      mMax[index] = max;
251
      return true;
252
      }
253

    
254
    return false;
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  void debugLists(String frameMarker)
260
    {
261
    debugListsGeneric(mDoneList,mToDoMap,frameMarker);
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
  static void debugCommonList()
267
    {
268
    debugListsGeneric(mCommonDoneList,mCommonToDoMap,"Common");
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  static void debugListsGeneric(LinkedList<InternalObject> list, HashMap<Long,Job> map,String frameMarker)
274
    {
275
    android.util.Log.e("Object", frameMarker);
276
    android.util.Log.e("Object", "  Done list:");
277

    
278
    for(InternalObject object : list)
279
      {
280
      object.print("  ");
281
      }
282

    
283
    android.util.Log.e("Object", "  ToDo list:");
284

    
285
    Job job;
286

    
287
    for(Long key: map.keySet())
288
      {
289
      job = map.get(key);
290
      job.object.print(job.action==InternalObject.JOB_CREATE ? " create":" delete");
291
      }
292
    }
293
  }
(14-14/16)