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.ide.eclipse.gltrace.state.transforms; 18 19 import com.android.ide.eclipse.gltrace.state.GLSparseArrayProperty; 20 import com.android.ide.eclipse.gltrace.state.IGLProperty; 21 22 /** 23 * A {@link SparseArrayElementAddTransform} changes given state by adding an 24 * element to a sparse array, if there is no item with the same key already. 25 */ 26 public class SparseArrayElementAddTransform implements IStateTransform { 27 private IGLPropertyAccessor mAccessor; 28 private int mKey; 29 private IGLProperty mOldValue; 30 31 public SparseArrayElementAddTransform(IGLPropertyAccessor accessor, int key) { 32 mAccessor = accessor; 33 mKey = key; 34 } 35 36 @Override 37 public void apply(IGLProperty currentState) { 38 GLSparseArrayProperty propertyArray = getArray(currentState); 39 if (propertyArray != null) { 40 mOldValue = propertyArray.getProperty(mKey); 41 if (mOldValue == null) { 42 // add only if there is no item with this key already present 43 propertyArray.add(mKey); 44 } 45 } 46 } 47 48 @Override 49 public void revert(IGLProperty currentState) { 50 GLSparseArrayProperty propertyArray = getArray(currentState); 51 if (propertyArray != null) { 52 if (mOldValue == null) { 53 // delete only if we actually added this key 54 propertyArray.delete(mKey); 55 } 56 } 57 } 58 59 @Override 60 public IGLProperty getChangedProperty(IGLProperty currentState) { 61 return getArray(currentState); 62 } 63 64 private GLSparseArrayProperty getArray(IGLProperty state) { 65 IGLProperty p = state; 66 67 if (mAccessor != null) { 68 p = mAccessor.getProperty(p); 69 } 70 71 if (p instanceof GLSparseArrayProperty) { 72 return (GLSparseArrayProperty) p; 73 } else { 74 return null; 75 } 76 } 77 } 78