Skip to main content
summaryrefslogtreecommitdiffstats
blob: ca8a67bc7364a46052a6eb2b77fe3e218e51d1b1 (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
158
159
160
/*
 * 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;

import org.eclipse.net4j.db.ddl.IDBSchemaElement;
import org.eclipse.net4j.db.ddl.IDBSchemaVisitor;
import org.eclipse.net4j.db.ddl.IDBSchemaVisitor.StopRecursion;
import org.eclipse.net4j.spi.db.ddl.InternalDBSchemaElement;
import org.eclipse.net4j.util.StringUtil;
import org.eclipse.net4j.util.collection.PositionProvider;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @since 4.2
 * @author Eike Stepper
 */
public abstract class DBSchemaElement extends DBNamedElement implements InternalDBSchemaElement
{
  private static final long serialVersionUID = 1L;

  private static final IDBSchemaElement[] NO_ELEMENTS = {};

  private transient IDBSchemaElement[] elements;

  public DBSchemaElement(String name)
  {
    super(name);
  }

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

  public final int compareTo(IDBSchemaElement element2)
  {
    int result = getSchemaElementType().compareTo(element2.getSchemaElementType());
    if (result == 0)
    {
      if (this instanceof PositionProvider && element2 instanceof PositionProvider)
      {
        PositionProvider withPosition1 = (PositionProvider)this;
        PositionProvider withPosition2 = (PositionProvider)element2;
        return withPosition1.getPosition() - withPosition2.getPosition();
      }

      result = getName().compareTo(element2.getName());
    }

    return result;
  }

  public final boolean isEmpty()
  {
    return getElements().length == 0;
  }

  public final IDBSchemaElement[] getElements()
  {
    if (elements == null)
    {
      List<IDBSchemaElement> result = new ArrayList<IDBSchemaElement>();
      collectElements(result);

      if (result.isEmpty())
      {
        elements = NO_ELEMENTS;
      }
      else
      {
        elements = result.toArray(new IDBSchemaElement[result.size()]);
        Arrays.sort(elements);
      }
    }

    return elements;
  }

  protected final void resetElements()
  {
    elements = null;
  }

  protected abstract void collectElements(List<IDBSchemaElement> elements);

  public final <T extends IDBSchemaElement> T getElement(Class<T> type, String name)
  {
    name = name(name);
    for (IDBSchemaElement element : getElements())
    {
      if (element.getName() == name && type.isAssignableFrom(element.getClass()))
      {
        @SuppressWarnings("unchecked")
        T result = (T)element;
        return result;
      }
    }

    return null;
  }

  public final void accept(IDBSchemaVisitor visitor)
  {
    try
    {
      doAccept(visitor);
    }
    catch (StopRecursion ex)
    {
      return;
    }

    for (IDBSchemaElement element : getElements())
    {
      element.accept(visitor);
    }
  }

  protected abstract void doAccept(IDBSchemaVisitor visitor);

  public void dump(Writer writer) throws IOException
  {
    SchemaElementType schemaElementType = getSchemaElementType();
    int level = schemaElementType.getLevel();
    for (int i = 0; i < level; i++)
    {
      writer.append("  ");
    }

    writer.append(schemaElementType.toString());
    writer.append(" ");
    writer.append(getName());
    dumpAdditionalProperties(writer);
    writer.append(StringUtil.NL);

    for (IDBSchemaElement element : getElements())
    {
      ((InternalDBSchemaElement)element).dump(writer);
    }
  }

  protected void dumpAdditionalProperties(Writer writer) throws IOException
  {
  }
}

Back to the top