01: /*
02: * Copyright 2006 Davide Deidda
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: /*
18: * Diff.java
19: *
20: * Created on 23 aprile 2005, 8.15
21: */
22:
23: package it.biobytes.ammentos.util;
24:
25: import it.biobytes.ammentos.*;
26: import java.util.*;
27: import java.util.logging.Logger;
28:
29: /**
30: *
31: * @author davide
32: */
33: public class Diff {
34:
35: /** Creates a new instance of Diff */
36: private Diff() {
37: }
38:
39: public static <T> List<Field> diff(Class<T> c, T obj1, T obj2)
40: throws PersistenceException {
41: List<Field> res = new ArrayList<Field>();
42: Metadata metadata = Ammentos.getMetadata(c);
43: for (Field field : metadata.getFields()) {
44: if (!fieldsEqual(field, obj1, obj2)) {
45: res.add(field);
46: }
47: }
48: return res;
49: }
50:
51: private static boolean fieldsEqual(Field field, Object obj1,
52: Object obj2) {
53: boolean res;
54: Object value1 = field.get(obj1);
55: Object value2 = field.get(obj2);
56:
57: if (value1 == null) {
58: res = (value2 == null);
59: } else {
60: res = value1.equals(value2);
61: }
62:
63: Logger.getLogger("ammentos").info(
64: "comparation fields: " + value1 + " vs " + value2
65: + ": " + res);
66: return res;
67: }
68:
69: }
|