Skip to main content
summaryrefslogtreecommitdiffstats
blob: fe27e48ccbd0db434accc672b6fa7dbe978a7bb4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.internal.db.ddl.delta;

import org.eclipse.net4j.db.ddl.IDBSchemaElement;
import org.eclipse.net4j.db.ddl.delta.IDBDelta;
import org.eclipse.net4j.db.ddl.delta.IDBPropertyDelta;
import org.eclipse.net4j.internal.db.DBElement;
import org.eclipse.net4j.util.CheckUtil;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Eike Stepper
 */
public abstract class DBDelta extends DBElement implements IDBDelta
{
  private static final long serialVersionUID = 1L;

  private IDBDelta parent;

  private String name;

  private ChangeKind changeKind;

  private Map<String, IDBPropertyDelta<?>> propertyDeltas = new HashMap<String, IDBPropertyDelta<?>>();

  public DBDelta(IDBDelta parent, String name, ChangeKind changeKind)
  {
    CheckUtil.checkArg(name, "name");

    this.parent = parent;
    this.name = name;
    this.changeKind = changeKind;
  }

  /**
   * Constructor for deserialization.
   */
  protected DBDelta()
  {
  }

  public IDBDelta getParent()
  {
    return parent;
  }

  public final String getName()
  {
    return name;
  }

  public final ChangeKind getChangeKind()
  {
    return changeKind;
  }

  public <T> IDBPropertyDelta<T> getPropertyDelta(String name)
  {
    @SuppressWarnings("unchecked")
    IDBPropertyDelta<T> propertyDelta = (IDBPropertyDelta<T>)propertyDeltas.get(name);
    return propertyDelta;
  }

  public <T> T getPropertyValue(String name)
  {
    return getPropertyValue(name, false);
  }

  public <T> T getPropertyValue(String name, boolean old)
  {
    IDBPropertyDelta<T> propertyDelta = getPropertyDelta(name);
    if (old)
    {
      return propertyDelta.getOldValue();
    }

    return propertyDelta.getValue();
  }

  public final Map<String, IDBPropertyDelta<?>> getPropertyDeltas()
  {
    return Collections.unmodifiableMap(propertyDeltas);
  }

  public final void addPropertyDelta(IDBPropertyDelta<?> propertyDelta)
  {
    propertyDeltas.put(propertyDelta.getName(), propertyDelta);
  }

  public static String getName(IDBSchemaElement element, IDBSchemaElement oldElement)
  {
    return oldElement == null ? element.getName() : oldElement.getName();
  }

  public static ChangeKind getChangeKind(Object object, Object oldObject)
  {
    return object == null ? ChangeKind.REMOVED : oldObject == null ? ChangeKind.ADDED : ChangeKind.CHANGED;
  }

  protected static <T extends IDBSchemaElement> void compare(T[] elements, T[] oldElements,
      SchemaElementComparator<T> comparator)
  {
    for (int i = 0; i < elements.length; i++)
    {
      T element = elements[i];
      String name = element.getName();

      T oldElement = findElement(oldElements, name);
      comparator.compare(element, oldElement);
    }

    for (int i = 0; i < oldElements.length; i++)
    {
      T oldElement = oldElements[i];
      String name = oldElement.getName();

      if (findElement(elements, name) == null)
      {
        comparator.compare(null, oldElement);
      }
    }
  }

  private static <T extends IDBSchemaElement> T findElement(T[] elements, String name)
  {
    for (int i = 0; i < elements.length; i++)
    {
      T element = elements[i];
      if (element.getName().equals(name))
      {
        return element;
      }
    }

    return null;
  }

  /**
   * @author Eike Stepper
   */
  public interface SchemaElementComparator<T extends IDBSchemaElement>
  {
    public void compare(T element, T oldElement);
  }
}

Back to the top