001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.jdbc.schema;
020:
021: import java.io.File;
022:
023: import org.apache.openjpa.lib.meta.SourceTracker;
024:
025: /**
026: * Represents a database sequence.
027: *
028: * @author Abe White
029: */
030: public class Sequence extends ReferenceCounter implements Comparable,
031: SourceTracker {
032:
033: private String _name = null;
034: private String _fullName = null;
035: private Schema _schema = null;
036: private String _schemaName = null;
037: private int _initial = 1;
038: private int _increment = 1;
039: private int _cache = 0;
040:
041: // keep track of source
042: private File _source = null;
043: private int _srcType = SRC_OTHER;
044:
045: /**
046: * Default constructor.
047: */
048: public Sequence() {
049: }
050:
051: /**
052: * Constructor.
053: *
054: * @param name the sequence name
055: * @param schema the sequence schema
056: */
057: public Sequence(String name, Schema schema) {
058: setName(name);
059: if (schema != null)
060: setSchemaName(schema.getName());
061: _schema = schema;
062: }
063:
064: /**
065: * Called when the sequence is removed from its schema.
066: */
067: void remove() {
068: _schema = null;
069: _fullName = null;
070: }
071:
072: /**
073: * Return the schema for the sequence.
074: */
075: public Schema getSchema() {
076: return _schema;
077: }
078:
079: /**
080: * The sequence's schema name.
081: */
082: public String getSchemaName() {
083: return _schemaName;
084: }
085:
086: /**
087: * The sequence's schema name. You can only call this method on sequences
088: * whose schema object is not set.
089: */
090: public void setSchemaName(String name) {
091: if (getSchema() != null)
092: throw new IllegalStateException();
093: _schemaName = name;
094: _fullName = null;
095: }
096:
097: /**
098: * Return the name of the sequence.
099: */
100: public String getName() {
101: return _name;
102: }
103:
104: /**
105: * Set the name of the sequence. This method can only be called on
106: * sequences that are not part of a schema.
107: */
108: public void setName(String name) {
109: if (getSchema() != null)
110: throw new IllegalStateException();
111: _name = name;
112: _fullName = null;
113: }
114:
115: /**
116: * Return the sequence name, including schema, using '.' as the
117: * catalog separator.
118: */
119: public String getFullName() {
120: if (_fullName == null) {
121: Schema schema = getSchema();
122: if (schema == null || schema.getName() == null)
123: _fullName = getName();
124: else
125: _fullName = schema.getName() + "." + getName();
126: }
127: return _fullName;
128: }
129:
130: /**
131: * The sequence's initial value.
132: */
133: public int getInitialValue() {
134: return _initial;
135: }
136:
137: /**
138: * The sequence's initial value.
139: */
140: public void setInitialValue(int initial) {
141: _initial = initial;
142: }
143:
144: /**
145: * The sequence's increment.
146: */
147: public int getIncrement() {
148: return _increment;
149: }
150:
151: /**
152: * The sequence's increment.
153: */
154: public void setIncrement(int increment) {
155: _increment = increment;
156: }
157:
158: /**
159: * The sequence's cache size.
160: */
161: public int getAllocate() {
162: return _cache;
163: }
164:
165: /**
166: * The sequence's cache size.
167: */
168: public void setAllocate(int cache) {
169: _cache = cache;
170: }
171:
172: public File getSourceFile() {
173: return _source;
174: }
175:
176: public Object getSourceScope() {
177: return null;
178: }
179:
180: public int getSourceType() {
181: return _srcType;
182: }
183:
184: public void setSource(File source, int srcType) {
185: _source = source;
186: _srcType = srcType;
187: }
188:
189: public String getResourceName() {
190: return getFullName();
191: }
192:
193: public int compareTo(Object other) {
194: String name = getFullName();
195: String otherName = ((Sequence) other).getFullName();
196: if (name == null && otherName == null)
197: return 0;
198: if (name == null)
199: return 1;
200: if (otherName == null)
201: return -1;
202: return name.compareTo(otherName);
203: }
204:
205: public String toString() {
206: return getFullName();
207: }
208: }
|