オブジェクト向け(抽象データ型)スタイルに基づくkwic実装
4570 ワード
ObjectOrientedMainクラス
Outputクラス
Alphabetizerクラス
Shiftクラス
Inputクラス
/**
*
*/
package com.jason.kwic.objectOriented;
import java.io.IOException;
/**
* @author jasonzhang
*
*/
public class ObjectOrientedMain {
public static void main(String[] args) throws IOException {
Output output = new Output();
Alphabetizer sorter = new Alphabetizer(output);
Shift shift = new Shift(sorter);
Input input = new Input(shift);
input.input("d:\\temp\\mykwic_in.txt");
shift.shift();
sorter.sort();
}
}
Outputクラス
/**
*
*/
package com.jason.kwic.objectOriented;
import java.util.ArrayList;
/**
* @author jasonzhang
*
*/
public class Output {
public void output(ArrayList<String> lines) {
System.out.println("output is");
for (String line : lines) {
System.out.println(line);
}
}
}
Alphabetizerクラス
/**
*
*/
package com.jason.kwic.objectOriented;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/**
* @author jasonzhang
*
*/
public class Alphabetizer {
private ArrayList<String> lines = new ArrayList<String>();
private Output output;
public Alphabetizer(Output output) {
this.output = output;
}
public void addLine(String line) {
this.lines.add(line);
}
public void sort() {
Collections.sort(this.lines, new AlphaabetizerComparator());
this.output.output(this.lines);
}
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;
}
}
}
Shiftクラス
/**
*
*/
package com.jason.kwic.objectOriented;
import java.util.ArrayList;
import java.util.StringTokenizer;
/**
* @author jasonzhang
*
*/
public class Shift {
private ArrayList<String> lines = new ArrayList<String>();
//private ArrayList<String> shifedLines = new ArrayList<String>();
private Alphabetizer sorter;
public Shift(Alphabetizer sorter) {
this.sorter = sorter;
}
public void addLine(String line) {
this.lines.add(line);
}
public void shift() {
for (String line : this.lines) {
this.shiftLine(line);
}
}
private void shiftLine(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");
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());
this.sorter.addLine(tempLine.toString());
tempLine.delete(0, tempLine.length());
}
}
}
Inputクラス
/**
*
*/
package com.jason.kwic.objectOriented;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
/**
* @author jasonzhang
*
*/
public class Input {
private Shift shift;
public Input(Shift shift) {
this.shift = shift;
}
public void input(String filePath) throws IOException {
File infile = new File(filePath);
Scanner sc = new Scanner(infile);
String tempLine = null;
System.out.println("input is");
while (sc.hasNextLine()) {
tempLine = sc.nextLine();
System.out.println(tempLine);
this.shift.addLine(tempLine);
}
}
}