1 /* 2 * Copyright (C) 2009 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 signature.compare.model.impl; 18 19 import java.lang.reflect.Field; 20 import java.util.Arrays; 21 import java.util.HashSet; 22 import java.util.LinkedList; 23 import java.util.List; 24 import java.util.Set; 25 26 import signature.compare.model.IDelta; 27 import signature.compare.model.DeltaType; 28 29 public abstract class SigDelta<T> implements IDelta<T> { 30 private T from; 31 private T to; 32 33 public SigDelta(T from, T to) { 34 this.from = from; 35 this.to = to; 36 } 37 38 public final T getFrom() { 39 return from; 40 } 41 42 public final T getTo() { 43 return to; 44 } 45 46 public final DeltaType getType() { 47 if (from == null && to != null) { 48 return DeltaType.ADDED; 49 } 50 if (from != null && to == null) { 51 return DeltaType.REMOVED; 52 } 53 return DeltaType.CHANGED; 54 } 55 56 private static <T extends IDelta<?>> Set<T> getDeltas(Set<T> deltas, 57 DeltaType type) { 58 Set<T> addedElements = new HashSet<T>(); 59 for (T delta : deltas) { 60 if (type.equals(delta.getType())) { 61 addedElements.add(delta); 62 } 63 } 64 return addedElements; 65 } 66 67 public static <T extends IDelta<?>> Set<T> getAdded(Set<T> deltas) { 68 return getDeltas(deltas, DeltaType.ADDED); 69 } 70 71 public static <T extends IDelta<?>> Set<T> getRemoved(Set<T> deltas) { 72 return getDeltas(deltas, DeltaType.REMOVED); 73 } 74 75 public static <T extends IDelta<?>> Set<T> getChanged(Set<T> deltas) { 76 return getDeltas(deltas, DeltaType.CHANGED); 77 } 78 79 @Override 80 public String toString() { 81 StringBuilder builder = new StringBuilder(); 82 builder.append(getClass().getSimpleName()); 83 builder.append(":\n"); 84 85 List<Field> allFields = new LinkedList<Field>(); 86 87 Class<?> actualClass = getClass(); 88 89 // add all fields / super classes also 90 do { 91 allFields.addAll(Arrays.asList(actualClass.getDeclaredFields())); 92 actualClass = actualClass.getSuperclass(); 93 } while (actualClass != Object.class); 94 95 builder.append("from: "); 96 builder.append(from); 97 builder.append("\nto: "); 98 builder.append(to); 99 builder.append("\n"); 100 try { 101 for (Field field : allFields) { 102 if (!ignore.contains(field.getName())) { 103 field.setAccessible(true); 104 Object delta = field.get(this); 105 if (delta != null) { 106 builder.append(field.getName()); 107 builder.append(":\n"); 108 builder.append(delta); 109 } 110 } 111 } 112 } catch (IllegalArgumentException e) { 113 e.printStackTrace(); 114 } catch (IllegalAccessException e) { 115 e.printStackTrace(); 116 } catch (SecurityException e) { 117 e.printStackTrace(); 118 } 119 120 return builder.toString(); 121 } 122 123 private static Set<String> ignore = new HashSet<String>(); 124 { 125 ignore.add("from"); 126 ignore.add("to"); 127 ignore.add("reason"); 128 ignore.add("ignore"); // =) 129 } 130 } 131