KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > media > Codec


1  /*
2   
3  This software is OSI Certified Open Source Software.
4  OSI Certified is a certification mark of the Open Source Initiative.
5  
6  The license (Mozilla version 1.0) can be read at the MMBase site.
7  See http://www.MMBase.org/license
8  */

9
10 package org.mmbase.applications.media;
11
12 import org.mmbase.util.logging.Logger;
13 import org.mmbase.util.logging.Logging;
14 import java.util.*;
15 // import org.mmbase.util.ConstantsBundle;
16

17 /**
18  * Makes the 'Format' constants available.
19  *
20  * @author Michiel Meeuwissen
21  * @version $Id: Codec.java,v 1.3 2005/01/30 16:46:36 nico Exp $
22  * @since MMBase-1.7
23  */

24 // See http://www.javaworld.com/javaworld/jw-07-1997/jw-07-enumerated.html
25
public final class Codec { // final class!!
26
private static Logger log = Logging.getLoggerInstance(Codec.class);
27
28     public final static String JavaDoc RESOURCE = "org.mmbase.applications.media.resources.codecs";
29     // in case you want i18ed format strings.
30

31     private static List codecs = new ArrayList(); // to make possible to get the Codec object by int.
32
private int number; // for storage
33
private String JavaDoc id; // for toString(), and as identifier in config file etc.
34
// Also sync with common extension?
35
// perhaps this could as well be used for storage
36

37     
38     private Codec(int n, String JavaDoc i) { // private constructor!!
39
number = n; id = i;
40         if (n >= 0) {
41             while (codecs.size() <= number) codecs.add(null);
42             codecs.set(number, this);
43         }
44     }
45     
46
47     // Codecs
48
public final static Codec UNKNOWN = new Codec(0, "unknown");
49     public final static Codec VORBIS = new Codec(1, "vorbis");
50     public final static Codec G2 = new Codec(2, "gd");
51     public final static Codec DIV3 = new Codec(3, "div3");
52     public final static Codec DIV4 = new Codec(4, "div4");
53     public final static Codec DIVX = new Codec(5, "divx");
54     public final static Codec MP1 = new Codec(6, "mp1");
55     public final static Codec MP2 = new Codec(7, "mp2");
56     public final static Codec MP3 = new Codec(8, "mp3");
57     public final static Codec MP4 = new Codec(9, "mp4");
58
59     // in contradiction to the example of the cited URL I prefer
60
// to state the number explicitely, because those numbers will
61
// appear in the database, so never may change (so don't
62
// determin the number automaticly
63

64
65     public int toInt() { return number; }
66     public String JavaDoc toString() { return id; }
67     public static Codec get(int i) {
68         if (i < 0) return UNKNOWN;
69         try {
70             return (Codec) codecs.get(i);
71         } catch (java.lang.IndexOutOfBoundsException JavaDoc e) {
72             return UNKNOWN;
73         }
74     }
75
76     public static Codec get(String JavaDoc id) {
77         id = id.toLowerCase();
78         Iterator i = codecs.iterator();
79         while (i.hasNext()) {
80             Codec codec = (Codec) i.next();
81             if(codec.toString().equals(id)) return codec;
82         }
83         log.error("Cannot convert codec (" + id + ") to number");
84         return UNKNOWN;
85     }
86     
87     public String JavaDoc getGUIIndicator(Locale locale) {
88         try {
89             ResourceBundle m = ResourceBundle.getBundle(RESOURCE, locale);
90             return m.getString("" + number);
91             //return ConstantsBundle.get(RESOURCE, this.getClass(), number, locale);
92
} catch (Exception JavaDoc e) {
93             return "UNKNOWN";
94         }
95     }
96     public boolean equals(Object JavaDoc o) {
97         if (o instanceof Codec) {
98             Codec c = (Codec) o;
99             return c.number == number;
100         }
101         return false;
102     }
103     
104     
105     /**
106      * @see java.lang.Object#hashCode()
107      */

108     public int hashCode() {
109         return number;
110     }
111 }
112     
113
Popular Tags