1 package net.sf.openrocket.database;
3 import java.util.ArrayList;
4 import java.util.Collections;
8 import net.sf.openrocket.logging.LogHelper;
9 import net.sf.openrocket.preset.ComponentPreset;
10 import net.sf.openrocket.startup.Application;
12 public abstract class ComponentPresetDatabase extends Database<ComponentPreset> implements ComponentPresetDao {
14 private static final LogHelper logger = Application.getLogger();
16 private volatile boolean startedLoading = false;
17 private volatile boolean endedLoading = false;
18 private final boolean asynchronous;
20 /** Set to true the first time {@link #blockUntilLoaded()} is called. */
21 protected volatile boolean inUse = false;
23 public ComponentPresetDatabase() {
25 this.asynchronous = false;
28 public ComponentPresetDatabase(boolean asynchronous ) {
30 this.asynchronous = asynchronous;
34 public List<ComponentPreset> listAll() {
40 public void insert( ComponentPreset preset ) {
45 public List<ComponentPreset> listForType( ComponentPreset.Type type ) {
48 return Collections.<ComponentPreset>emptyList();
51 List<ComponentPreset> result = new ArrayList<ComponentPreset>(list.size()/6);
53 for( ComponentPreset preset : list ) {
54 if ( preset.get(ComponentPreset.TYPE).equals(type) ) {
63 * Return a list of component presets based on the type.
64 * All components returned will be of Type type.
67 * @param favorite if true, only return the favorites. otherwise return all matching.
71 public List<ComponentPreset> listForType( ComponentPreset.Type type, boolean favorite ) {
75 return listForType(type);
78 List<ComponentPreset> result = new ArrayList<ComponentPreset>(list.size()/6);
80 Set<String> favorites = Application.getPreferences().getComponentFavorites(type);
82 for( ComponentPreset preset : list ) {
83 if ( preset.get(ComponentPreset.TYPE).equals(type) && favorites.contains(preset.preferenceKey())) {
91 public List<ComponentPreset> listForTypes( ComponentPreset.Type ... type ) {
94 if( type == null || type.length == 0 ) {
95 return Collections.<ComponentPreset>emptyList();
98 if (type.length == 1 ) {
99 return listForType(type[0]);
102 List<ComponentPreset> result = new ArrayList<ComponentPreset>(list.size()/6);
104 for( ComponentPreset preset : list ) {
105 ComponentPreset.Type presetType = preset.get(ComponentPreset.TYPE);
106 typeLoop: for( int i=0; i<type.length; i++ ) {
107 if ( presetType.equals(type[i]) ) {
109 break typeLoop; // from inner loop.
118 public List<ComponentPreset> listForTypes( List<ComponentPreset.Type> types ) {
120 return listForTypes( (ComponentPreset.Type[]) types.toArray() );
124 public List<ComponentPreset> find(String manufacturer, String partNo) {
126 List<ComponentPreset> presets = new ArrayList<ComponentPreset>();
127 for( ComponentPreset preset : list ) {
128 if ( preset.getManufacturer().getSimpleName().equals(manufacturer) && preset.getPartNo().equals(partNo) ) {
136 public void setFavorite( ComponentPreset preset, ComponentPreset.Type type, boolean favorite ) {
138 Application.getPreferences().setComponentFavorite( preset, type, favorite );
139 this.fireAddEvent(preset);
144 * Used for loading the component preset database. This method will be called in a background
145 * thread to load the presets asynchronously.
147 protected abstract void load();
150 * Start loading the presets.
152 * @throws IllegalStateException if this method has already been called.
154 public void startLoading() {
155 if (startedLoading) {
156 throw new IllegalStateException("Already called startLoading");
158 startedLoading = true;
160 new LoadingThread().start();
164 synchronized (this) {
171 * Background thread for loading the presets.
173 private class LoadingThread extends Thread {
175 private LoadingThread() {
176 this.setName("PresetLoadingThread");
177 this.setPriority(MIN_PRIORITY);
186 * Block the current thread until loading of the presets has been completed.
188 * @throws IllegalStateException if startLoading() has not been called.
190 public void blockUntilLoaded() {
192 if (!startedLoading) {
193 throw new IllegalStateException("startLoading() has not been called");
196 synchronized (this) {
197 while (!endedLoading) {
200 } catch (InterruptedException e) {
201 logger.warn("InterruptedException occurred, ignoring", e);