001 /* ListResourceBundle -- a resource bundle build around a list 002 Copyright (C) 1998, 1999, 2001, 2002, 2005 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 039 package java.util; 040 041 /** 042 * A <code>ListResouceBundle</code> provides an easy way, to create your own 043 * resource bundle. It is an abstract class that you can subclass. You should 044 * then overwrite the getContents method, that provides a key/value list. 045 * 046 * <p>The key/value list is a two dimensional list of Object. The first 047 * dimension ranges over the resources. The second dimension ranges from 048 * zero (key) to one (value). The keys must be of type String, and they are 049 * case-sensitive. For example: 050 * 051 <br><pre>public class MyResources 052 extends ListResourceBundle 053 { 054 public Object[][] getContents() 055 { 056 return contents; 057 } 058 059 static final Object[][] contents = 060 { 061 // LOCALIZED STRINGS 062 {"s1", "The disk \"{1}\" contains {0}."}, // MessageFormat pattern 063 {"s2", "1"}, // location of {0} in pattern 064 {"s3", "My Disk"}, // sample disk name 065 {"s4", "no files"}, // first ChoiceFormat choice 066 {"s5", "one file"}, // second ChoiceFormat choice 067 {"s6", "{0,number} files"} // third ChoiceFormat choice 068 {"s7", "3 Mar 96"}, // sample date 069 {"s8", new Dimension(1,5)} // real object, not just string 070 // END OF LOCALIZED MATERIAL 071 }; 072 }</pre> 073 * 074 * @author Jochen Hoenicke 075 * @author Eric Blake (ebb9@email.byu.edu) 076 * @see Locale 077 * @see PropertyResourceBundle 078 * @since 1.1 079 * @status updated to 1.4 080 */ 081 public abstract class ListResourceBundle extends ResourceBundle 082 { 083 /** 084 * The constructor. It does nothing special. 085 */ 086 public ListResourceBundle() 087 { 088 } 089 090 /** 091 * Gets a resource for a given key. This is called by <code>getObject</code>. 092 * 093 * @param key the key of the resource 094 * @return the resource for the key, or null if it doesn't exist 095 */ 096 public final Object handleGetObject(String key) 097 { 098 Object[][] contents = getContents(); 099 int i = contents.length; 100 while (--i >= 0) 101 if (key.equals(contents[i][0])) 102 return contents[i][1]; 103 return null; 104 } 105 106 /** 107 * This method should return all keys for which a resource exists. 108 * 109 * @return an enumeration of the keys 110 */ 111 public Enumeration<String> getKeys() 112 { 113 // We make a new Set that holds all the keys, then return an enumeration 114 // for that. This prevents modifications from ruining the enumeration, 115 // as well as ignoring duplicates. 116 final Object[][] contents = getContents(); 117 Set<String> s = new HashSet<String>(); 118 int i = contents.length; 119 while (--i >= 0) 120 s.add((String) contents[i][0]); 121 ResourceBundle bundle = parent; 122 // Eliminate tail recursion. 123 while (bundle != null) 124 { 125 Enumeration<String> e = bundle.getKeys(); 126 while (e.hasMoreElements()) 127 s.add(e.nextElement()); 128 bundle = bundle.parent; 129 } 130 return Collections.enumeration(s); 131 } 132 133 /** 134 * Gets the key/value list. You must override this method, and should not 135 * provide duplicate keys or null entries. 136 * 137 * @return a two dimensional list of String key / Object resouce pairs 138 */ 139 protected abstract Object[][] getContents(); 140 } // class ListResourceBundle