标签:
ImplicitlyCallMain类
/** * */ package com.jason.kwic.implicitlyCall; import java.io.File; import java.io.IOException; import java.util.List; import java.util.Scanner; /** * @author jasonzhang * */ public class ImplicitlyCallMain { public static void main(String[] args) throws IOException { Shift shift = new Shift(); Alphabetizer sort = new Alphabetizer(); //增加观察者 shift.addObserver(sort); File infile = new File("d:\\temp\\mykwic_in.txt"); Scanner sc = new Scanner(infile); String tempLine = null; System.out.println("input is"); while (sc.hasNextLine()) { tempLine = sc.nextLine(); System.out.println(tempLine); shift.addLine(tempLine); } List<String> sortedLines = sort.getSortedLines(); System.out.println("output is"); for (String line : sortedLines) { System.out.println(line); } } }
Shift类
/** * */ package com.jason.kwic.implicitlyCall; import java.util.ArrayList; import java.util.List; import java.util.Observable; import java.util.StringTokenizer; /** * @author jasonzhang * */ public class Shift extends Observable { public void addLine(String line) { ArrayList<String> wordList = new ArrayList<String>(); StringTokenizer st = new StringTokenizer(line, " "); while (st.hasMoreTokens()) { wordList.add(st.nextToken()); } int wordNums = wordList.size(); StringBuilder tempLine = new StringBuilder(); //System.out.println("shifed is"); List<String> shiftedLines = new ArrayList<String>(); for (int i=0; i<wordNums; i++) { for (int j=(wordNums - 1 -i); j < wordNums; j++) { tempLine.append(wordList.get(j)).append(‘ ‘); } for (int k=0; k<(wordNums - 1 -i); k++) { if (k != (wordNums - i - 2)) { tempLine.append(wordList.get(k)).append(‘ ‘); } else { tempLine.append(wordList.get(k)); } } //System.out.println(tempLine.toString()); //this.shifedLines.add(tempLine.toString()); shiftedLines.add(tempLine.toString()); tempLine.delete(0, tempLine.length()); } super.setChanged(); super.notifyObservers(shiftedLines); } }
Alphabetizer类
/** * */ package com.jason.kwic.implicitlyCall; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Observable; import java.util.Observer; /** * @author jasonzhang * */ public class Alphabetizer implements Observer { private List<String> sortedLines = new ArrayList<String>(); /* (non-Javadoc) * @see java.util.Observer#update(java.util.Observable, java.lang.Object) */ @Override public void update(Observable o, Object arg) { List<String> lines = (List<String>) arg; this.sortedLines.addAll(lines); Collections.sort(this.sortedLines, new AlphaabetizerComparator()); } public List<String> getSortedLines() { return this.sortedLines; } private class AlphaabetizerComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { if (o1 == null || o2 == null) { throw new NullPointerException(); } int compareValue = 0; char o1FirstCharacter = o1.charAt(0); char o2FirstCharacter = o2.charAt(0); if(this.isLetter(o1FirstCharacter) && this.isLetter(o2FirstCharacter)) { //如果是小写的字母的值,则转成对应的大写的字母的值 o1FirstCharacter = this.toUpperCase(o1FirstCharacter); o2FirstCharacter = this.toUpperCase(o2FirstCharacter); compareValue = o1FirstCharacter - o2FirstCharacter; } else { throw new RuntimeException("必须是字母"); } return compareValue; } private boolean isLetter(char c) { return (c >= 65 && c <= 90) || (c >= 97 && c <= 122); } private char toUpperCase(char c) { if (Character.isLowerCase(c)) { return Character.toUpperCase(c); } return c; } } }
实际上就是观察者模式,使用了jdk中的Observable和Observer
标签:
原文地址:http://my.oschina.net/u/914897/blog/410165