Merge commit '42b2e5ca519766e37ce6941ba4faecc9691cc403' into upstream
[debian/openrocket] / core / src / net / sf / openrocket / motor / Manufacturer.java
index 831181bbfae3033dd04a38b6c2345243ad15ed3b..5d562ed7c72799e7f88a26a77d45e27281dfe326 100644 (file)
@@ -4,7 +4,9 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Locale;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * Class containing information about motor manufacturers.
@@ -12,8 +14,22 @@ import java.util.Set;
  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
  */
 public class Manufacturer {
-       
-       private static Set<Manufacturer> manufacturers = new HashSet<Manufacturer>();
+
+       private static class ManufacturerList extends ConcurrentHashMap<String,Manufacturer> {
+               
+               void add( Manufacturer m ) {
+                       for( String s : m.getSearchNames() ) {
+                               Manufacturer previousRegistered;
+                               if ( (previousRegistered = putIfAbsent( s, m )) != null ) {
+                                       throw new IllegalStateException("Manufacturer name clash between " +
+                                                       "manufacturers " + previousRegistered + " and " + m + " name " + s);
+                               }
+                       }
+               }
+               
+       }
+       private static ManufacturerList manufacturers = new ManufacturerList();
+
        static {
                
                // AeroTech has many name combinations...
@@ -98,24 +114,10 @@ public class Manufacturer {
                                "WECO", "WECO FEUERWERKS", "SF", "SACHSEN", "SACHSEN FEUERWERK",
                                "SACHSEN FEUERWERKS"));
                
-
-               // Check that no duplicates have appeared
-               for (Manufacturer m1 : manufacturers) {
-                       for (Manufacturer m2 : manufacturers) {
-                               if (m1 == m2)
-                                       continue;
-                               for (String name : m1.getAllNames()) {
-                                       if (m2.matches(name)) {
-                                               throw new IllegalStateException("Manufacturer name clash between " +
-                                                               "manufacturers " + m1 + " and " + m2 + " name " + name);
-                                       }
-                               }
-                       }
-               }
        }
        
-
-
+       
+       
        private final String displayName;
        private final String simpleName;
        private final Set<String> allNames;
@@ -181,6 +183,9 @@ public class Manufacturer {
                return allNames;
        }
        
+       Set<String> getSearchNames() {
+               return searchNames;
+       }
        
        /**
         * Return the motor type that this manufacturer produces if it produces only one motor type.
@@ -228,22 +233,29 @@ public class Manufacturer {
         * @param name  the manufacturer name to search for.
         * @return              the Manufacturer object corresponding the name.
         */
-       public static synchronized Manufacturer getManufacturer(String name) {
-               for (Manufacturer m : manufacturers) {
-                       if (m.matches(name))
-                               return m;
+       public static Manufacturer getManufacturer(String name) {
+               String searchString = generateSearchString(name);
+               Manufacturer m = manufacturers.get(searchString);
+               if ( m != null ) {
+                       return m;
+               }
+
+               m = new Manufacturer(name.trim(), name.trim(), Motor.Type.UNKNOWN);
+
+               // We need some additional external synchronization here so we lock on the manufacturers.
+               synchronized( manufacturers ) {
+                       Manufacturer retest = manufacturers.get(searchString);
+                       if ( retest != null ) {
+                               // it exists now.
+                               return retest;
+                       }
+                       manufacturers.add(m);
                }
-               
-               Manufacturer m = new Manufacturer(name.trim(), name.trim(), Motor.Type.UNKNOWN);
-               manufacturers.add(m);
                return m;
        }
        
-       
-
-
-       private String generateSearchString(String str) {
-               return str.toLowerCase().replaceAll("[^a-zA-Z0-9]+", " ").trim();
+       private static String generateSearchString(String str) {
+               return str.toLowerCase(Locale.getDefault()).replaceAll("[^a-zA-Z0-9]+", " ").trim();
        }
        
 }