net.ucanaccess.triggers.AutoNumberManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ucanaccess Show documentation
Show all versions of ucanaccess Show documentation
An open source pure Java JDBC Driver implementation which allows Java developers and JDBC client programs (e.g., Open Office, Libre Office, Squirrel SQL) to read/write Microsoft Access databases.
The newest version!
/*
Copyright (c) 2012 Marco Amadei.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package net.ucanaccess.triggers;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import com.healthmarketscience.jackcess.Column;
import com.healthmarketscience.jackcess.impl.ColumnImpl;
import net.ucanaccess.jdbc.DBReference;
import net.ucanaccess.jdbc.OnReloadReferenceListener;
public final class AutoNumberManager {
// Consider replacing AtomicInteger with a custom wrapper around an 'int' if performance
// becomes an issue. Never use an Integer here because Integer is an immutable object.
private static final Map REGISTER = new HashMap();
static {
DBReference.addOnReloadRefListener(new OnReloadReferenceListener() {
@Override
public void onReload() {
// Must call AutoNumberManager.clear() for proper thread synchronization.
// Do not call register.clear() directly.
clear();
}
});
}
private AutoNumberManager() {
}
/** Clears all AutoNumber column seeds to 0. */
static synchronized void clear() {
REGISTER.clear();
}
/** Returns the next AutoNumber value, and increments the seed. */
static synchronized int getNext(Column cl) {
// Note: This code assumes *sequential* integer AutoNumber values.
// (Access also supports *random* integer AutoNumber values, but they
// are not very common.)
ColumnImpl ci = (ColumnImpl) cl;
AtomicInteger next = REGISTER.get(ci);
if (next == null) {
next = new AtomicInteger((Integer) ci.getAutoNumberGenerator().getLast());
REGISTER.put(ci, next);
}
return next.incrementAndGet();
}
/** Sets the AutoNumber seed to {@code newVal}. */
public static synchronized void reset(Column cl, int newVal) {
REGISTER.put(cl, new AtomicInteger(newVal));
}
/** Bumps the AutoNumber seed to {@code newVal} if it is higher than the existing one. */
public static synchronized void bump(Column cl, int newVal) {
ColumnImpl ci = (ColumnImpl) cl;
AtomicInteger next = REGISTER.get(ci);
if (next == null) {
next = new AtomicInteger((Integer) ci.getAutoNumberGenerator().getLast());
REGISTER.put(ci, next);
}
if (newVal > next.get()) {
next.set(newVal);
}
}
}