1 package com.billkuker.rocketry.motorsim.fuel;
\r
4 import java.io.FileInputStream;
\r
5 import java.io.FileNotFoundException;
\r
6 import java.io.IOException;
\r
8 import java.net.URISyntaxException;
\r
9 import java.util.Collections;
\r
10 import java.util.HashMap;
\r
11 import java.util.HashSet;
\r
12 import java.util.Map;
\r
13 import java.util.Set;
\r
15 import com.billkuker.rocketry.motorsim.Fuel;
\r
16 import com.billkuker.rocketry.motorsim.io.MotorIO;
\r
18 public class FuelResolver {
\r
19 public static class FuelNotFound extends Exception {
\r
20 private static final long serialVersionUID = 1L;
\r
23 public static interface FuelsChangeListener {
\r
24 public void fuelsChanged();
\r
27 private static Set<FuelsChangeListener> listeners = new HashSet<FuelResolver.FuelsChangeListener>();
\r
28 private static Map<URI, Fuel> fuels = new HashMap<URI, Fuel>();
\r
29 private static Map<Fuel, URI> uris = new HashMap<Fuel, URI>();
\r
33 add(new KNSB(), new URI("motorsim:KNSB"));
\r
34 add(new KNDX(), new URI("motorsim:KNDX"));
\r
35 add(new KNSU(), new URI("motorsim:KNSU"));
\r
36 add(new KNER(), new URI("motorsim:KNER"));
\r
37 } catch (URISyntaxException e) {
\r
38 e.printStackTrace();
\r
42 public static void addFuelsChangeListener(FuelsChangeListener l){
\r
46 public static void removeFuelsChangeListener(FuelsChangeListener l){
\r
47 listeners.remove(l);
\r
50 public static Map<URI, Fuel> getFuelMap(){
\r
51 return Collections.unmodifiableMap(fuels);
\r
54 public static URI getURI( Fuel f ){
\r
58 public static Fuel getFuel(URI u) throws FuelNotFound {
\r
59 if (fuels.containsKey(u))
\r
60 return fuels.get(u);
\r
61 return tryResolve(u);
\r
64 private static Fuel tryResolve(URI u) throws FuelNotFound {
\r
65 File f = new File(u);
\r
67 Fuel fuel = MotorIO.readFuel(new FileInputStream(f));
\r
70 } catch (FileNotFoundException e) {
\r
71 throw new FuelNotFound();
\r
72 } catch (IOException e) {
\r
73 throw new FuelNotFound();
\r
77 public static void add(Fuel f, URI uri) {
\r
80 for ( FuelsChangeListener l : listeners )
\r