View Javadoc

1   package be.bastard.mtracker;
2   
3   import static be.bastard.mtracker.Dvd.ContentType;
4   import be.bastard.mtracker.external.PlatformIndependentGuidGen;
5   
6   /***
7    * Factory class for <code>MediaItem</code>s.
8    *
9    * @author Jo Vandermeeren
10   * @since 1.0-SNAPSHOT
11   */
12  public class MediaFactory {
13      protected static long mediaCatalogCounter;
14      private static long dvdCounter;
15      public final static ContentType defaultDvdContentType = ContentType.MIXED;
16  
17      public static MediaId createMediaId() {
18          String random = PlatformIndependentGuidGen.getInstance().genNewGuid();
19          return new GeneratedUniqueId(random);
20      }
21  
22      /***
23       * Factory method to create a default media catalog.
24       * <br/>
25       * The name of the catalog will be "catalog-x", where 'x' is the
26       * number of catalogs that has been created with the factory method.
27       *
28       * @return Default MediaCatalog implementation
29       */
30      public static MediaCatalog createMediaCatalog() {
31          return createMediaCatalog("catalog-" + mediaCatalogCounter++);
32      }
33  
34      /***
35       * Factory method to create a media catalog with name <code>catalogName</name>.
36       *
37       * @param catalogName The name you want to give to the catalog
38       * @return MediaCatalog implementation with name <code>catalogName</code>
39       */
40      public static MediaCatalog createMediaCatalog(String catalogName) {
41          return new MediaCatalog(catalogName);
42      }
43  
44      /***
45       * Factory method to create a default DVD.
46       * <br />
47       * The DVD's name will be "dvd-x", where 'x' is the number of DVD's
48       * that have been created with the factory method.
49       * <br />
50       * The DVD's content type will be set to {@link Dvd.ContentType#MIXED}
51       *
52       * @return Default Dvd implementation
53       */
54      public static Dvd createDvd() {
55          return createDvd(defaultDvdContentType);
56      }
57  
58      /***
59       * Factory method to create a DVD with name <code>title</code> and
60       * default content type.
61       * <br />
62       * The DVD's content type will be set to {@link Dvd.ContentType#MIXED}
63       *
64       * @param title The name of this DVD
65       * @return Dvd implementation with name <code>title</code> and default content type
66       */
67      public static Dvd createDvd(String title) {
68          return createDvd(title, defaultDvdContentType);
69      }
70  
71      /***
72       * Factory method to create a DVD with default name and specific content type.
73       *
74       * @param contentType The type of content this DVD contains
75       * @return Dvd implementation with specific content type
76       */
77      public static Dvd createDvd(ContentType contentType) {
78          long dvdCount;
79          synchronized (MediaFactory.class) {
80              dvdCount = dvdCounter++;
81          }
82          return createDvd("dvd-" + dvdCount, contentType);
83      }
84  
85      /***
86       * Factory method to create a DVD with specific name and content type.
87       *
88       * @param title The name of this DVD
89       * @param contentType The type of content this DVD contains
90       * @return Dvd implementation with specific name and content type
91       */
92      public static Dvd createDvd(String title, ContentType contentType) {
93          return new Dvd(title, contentType);
94      }
95  
96      /***
97       * Helper method to create a unique ID.
98       */
99      private static class GeneratedUniqueId implements MediaId {
100         private final String generatedString;
101 
102         private GeneratedUniqueId(String generatedString) {
103             this.generatedString = generatedString;
104         }
105 
106         public boolean equals(MediaId id) {
107             return id.toInt() == this.toInt();
108         }
109 
110         public int toInt() {
111             return generatedString.hashCode();
112         }
113 
114         public int compareTo(MediaId id) {
115             return this.toInt() - id.toInt();
116         }
117 
118         public String toString() {
119             return generatedString;
120         }
121     }
122 }