All pastes #620143 Raw Edit

List searching

public java v1 · immutable
·published 2007-07-15 08:34 UTC
rendered paste body
import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Map;/** * @author Richard L. Burton III * @version 1.0 */public class SearchList extends ArrayList<String> {    private Map<String, Integer> indices = new HashMap<String, Integer>();    @Override    public boolean add(String s) {        if (!indices.containsKey(s)) {            indices.put(s, size() + 1);            return super.add(s);        }        return false;    }    public ArrayList<Integer> search(String message) {        ArrayList<Integer> indexes = new ArrayList<Integer>();        String[] temp = message.split(" ");        for (String aTemp : temp) {            String word = aTemp.replaceAll("\u002D", " ");            Integer index = indices.get(word);            if (index != null) {                indexes.add(index);            }        }        return indexes;    }    public boolean contains(String message) {        return indices.get(message) != null;    }    /**     * Removes everything that is not a letter or a number.     * @param s     * @return     */    public static String remove(String s) {        s = s.replaceAll("[^a-zA-Z_0-9\u002D]", " ");        return s;    }    public void save(String fileName) {//		Filesystem.writeFile(fileName, this.list);    }    public ArrayList<Integer> searchString(String message) {        ArrayList<Integer> indices = new ArrayList<Integer>();        for (int i = 0; i < size(); i++) {            if (get(i).contains(message)) {                indices.add(i);            }        }        return indices;    }    @SuppressWarnings("deprecation")    public static String replace(String s, String name, String sender, String message, String channel) {        // If we have <nick> present, replace it with the sender's nick.        if (s.indexOf("<nick>") >= 0) {            s = s.replace("<nick>", sender);        }        if (s.indexOf("<channel>") >= 0) {            s = s.replace("<channel>", channel);        }        if (s.indexOf("<message>") >= 0) {            s = s.replace("<message>", message);        }        if (s.indexOf("<time>") >= 0) {            s = s.replace("<time>", (new Date()).toString());        }        if (s.indexOf("<random>") >= 0) {            s = s.replace("<random>", ((int) Math.floor(Math.random() * 100)) + "");        }        if (s.indexOf("<name>") >= 0) {            s = s.replace("<name>", name);        }        if (s.indexOf("<time of day>") >= 0) {            int hours = new Date().getHours();            String replace = "time";            if (hours < 11) {                replace = "morning";            } else if (hours > 13 && hours < 19) {                replace = "afternoon";            } else if (hours >= 19) {                replace = "evening";            } else {                replace = "day";            }            s = s.replace("<time of day>", replace);        }        return s;    }    public static void main(String[] args) {        SearchList list = new SearchList();        list.add("richard");        list.add("test");        long start = System.currentTimeMillis();        for (int index = 0; index < 10000; index++) {            ArrayList<Integer> indexes = list.search("one other test two");            for (Integer i : indexes) {                assert i == 2;            }        }        System.out.println("System.currentTimeMillis() - start = " + (System.currentTimeMillis() - start));    }}