1 package net.sf.openrocket.database;
3 import java.util.ArrayList;
4 import java.util.Collections;
7 import net.sf.openrocket.logging.LogHelper;
8 import net.sf.openrocket.preset.ComponentPreset;
9 import net.sf.openrocket.startup.Application;
11 public abstract class ComponentPresetDatabase extends Database<ComponentPreset> implements ComponentPresetDao {
13 private static final LogHelper logger = Application.getLogger();
15 private volatile boolean startedLoading = false;
16 private volatile boolean endedLoading = false;
17 private final boolean asynchronous;
19 /** Set to true the first time {@link #blockUntilLoaded()} is called. */
20 protected volatile boolean inUse = false;
22 public ComponentPresetDatabase() {
24 this.asynchronous = false;
27 public ComponentPresetDatabase(boolean asynchronous ) {
29 this.asynchronous = asynchronous;
33 public List<ComponentPreset> listAll() {
39 public void insert( ComponentPreset preset ) {
44 public List<ComponentPreset> listForType( ComponentPreset.Type type ) {
47 return Collections.<ComponentPreset>emptyList();
50 List<ComponentPreset> result = new ArrayList<ComponentPreset>(list.size()/6);
52 for( ComponentPreset preset : list ) {
53 if ( preset.get(ComponentPreset.TYPE).equals(type) ) {
62 * Return a list of component presets based on the type.
63 * All components returned will be of Type type.
66 * @param favorite if true, only return the favorites. otherwise return all matching.
70 public List<ComponentPreset> listForType( ComponentPreset.Type type, boolean favorite ) {
74 return listForType(type);
77 List<ComponentPreset> result = new ArrayList<ComponentPreset>(list.size()/6);
79 for( ComponentPreset preset : list ) {
80 if ( preset.isFavorite() && preset.get(ComponentPreset.TYPE).equals(type) ) {
88 public List<ComponentPreset> listForTypes( ComponentPreset.Type ... type ) {
91 if( type == null || type.length == 0 ) {
92 return Collections.<ComponentPreset>emptyList();
95 if (type.length == 1 ) {
96 return listForType(type[0]);
99 List<ComponentPreset> result = new ArrayList<ComponentPreset>(list.size()/6);
101 for( ComponentPreset preset : list ) {
102 ComponentPreset.Type presetType = preset.get(ComponentPreset.TYPE);
103 typeLoop: for( int i=0; i<type.length; i++ ) {
104 if ( presetType.equals(type[i]) ) {
106 break typeLoop; // from inner loop.
115 public List<ComponentPreset> listForTypes( List<ComponentPreset.Type> types ) {
117 return listForTypes( (ComponentPreset.Type[]) types.toArray() );
121 public List<ComponentPreset> find(String manufacturer, String partNo) {
123 List<ComponentPreset> presets = new ArrayList<ComponentPreset>();
124 for( ComponentPreset preset : list ) {
125 if ( preset.getManufacturer().getSimpleName().equals(manufacturer) && preset.getPartNo().equals(partNo) ) {
133 public void setFavorite( ComponentPreset preset, boolean favorite ) {
135 preset.setFavorite(favorite);
136 Application.getPreferences().setComponentFavorite( preset, favorite );
137 this.fireAddEvent(preset);
142 * Used for loading the component preset database. This method will be called in a background
143 * thread to load the presets asynchronously.
145 protected abstract void load();
148 * Start loading the presets.
150 * @throws IllegalStateException if this method has already been called.
152 public void startLoading() {
153 if (startedLoading) {
154 throw new IllegalStateException("Already called startLoading");
156 startedLoading = true;
158 new LoadingThread().start();
162 synchronized (this) {
169 * Background thread for loading the presets.
171 private class LoadingThread extends Thread {
179 * Block the current thread until loading of the presets has been completed.
181 * @throws IllegalStateException if startLoading() has not been called.
183 public void blockUntilLoaded() {
185 if (!startedLoading) {
186 throw new IllegalStateException("startLoading() has not been called");
189 synchronized (this) {
190 while (!endedLoading) {
193 } catch (InterruptedException e) {
194 logger.warn("InterruptedException occurred, ignoring", e);