Write a simple text editor, which stores astring of characters using Java Linked
ID: 3758214 • Letter: W
Question
Write a simple text editor, which stores astring of characters using Java LinkedPositionalList (see pastebin links), together with a cursor object that highlights the position of some character in the string (or possibly the position before the rst character). Your editor should support the following operations and redisplay thecurrent text (that is, the list) after performing any one of them.
• left: Move cursor left one character (or nothing if at the beginning)
• right: Move cursor right one character (or do nothing if at the end)
• delete: Delete the character to the right of the cursor (or do nothing if at the end)
• insert c: Insert the character c just after the cursor
Use > for cursor so if your characters are "ABCD" and cursor at the beginning then ">ABCD".
Then modify the list to add two sequence based operations: indexOf(p) to return an index/rank of a position and atIndex(i) to return position of ith element.
Position interface: http://pastebin.com/BfV6d97P
PositionalList interface: http://pastebin.com/MMghBg9d
LinkedPositionalList class: http://pastebin.com/jWzmFT80
Posting pastebin links would help a lot for easier syntax reading!
Explanation / Answer
The following is the text editor program in java
which serves your purpose.
import static com.github.drapostolos.typeparser.TypeParserUtility.*;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
public final class StringToTypeParser {
private final Map<Type, TypeParser<?>> typeParsers;
final Splitter splitter;
final Splitter keyValuePairSplitter;
final InputPreprocessor inputPreprocessor;;
public static StringToTypeParserBuilder newBuilder() {
return new StringToTypeParserBuilder();
}
StringToTypeParser(StringToTypeParserBuilder builder) {
this.typeParsers = Collections.unmodifiableMap(new HashMap<Type, TypeParser<?>>(builder.typeParsers));
this.splitter = builder.splitter;
this.keyValuePairSplitter = builder.keyValuePairSplitter;
this.inputPreprocessor = builder.inputPreprocessor;
}
public <T> T parse(String input, Class<T> targetType) {
if (input == null) {
throw new NullPointerException(makeNullArgumentErrorMsg("input"));
}
if (targetType == null) {
throw new NullPointerException(makeNullArgumentErrorMsg("targetType"));
}
@SuppressWarnings("unchecked")
T temp = (T) parseType2(input, targetType);
return temp;
}
public <T> T parse(String input, GenericType<T> genericType) {
if (input == null) {
throw new NullPointerException(makeNullArgumentErrorMsg("input"));
}
if (genericType == null) {
throw new NullPointerException(makeNullArgumentErrorMsg("genericType"));
}
@SuppressWarnings("unchecked")
T temp = (T) parseType2(input, genericType.getType());
return temp;
}
public Object parseType(String input, Type targetType) {
if (input == null) {
throw new NullPointerException(makeNullArgumentErrorMsg("input"));
}
if (targetType == null) {
throw new NullPointerException(makeNullArgumentErrorMsg("targetType"));
}
return parseType2(input, targetType);
}
private Object parseType2(final String input, Type targetType) {
String preprocessedInput = preProcessInputString(input, targetType);
if(preprocessedInput == null){
if (isPrimitive(targetType)) {
String message = "'%s' primitive can not be set to null. Input: "%s"; Preprocessed input: '%s'";
throw new IllegalArgumentException(String.format(message, targetType, input, preprocessedInput));
}
return null;
}
if(typeParsers.containsKey(targetType)){
return invokeTypeParser(preprocessedInput, targetType, targetType);
}
if(targetType instanceof ParameterizedType){
ParameterizedType type = (ParameterizedType) targetType;
Class<?> rawType = (Class<?>) type.getRawType();
if(List.class.isAssignableFrom(rawType)){
return invokeTypeParser(preprocessedInput, TypeParsers.ANY_LIST, targetType);
}
if(Set.class.isAssignableFrom(rawType)){
return invokeTypeParser(preprocessedInput, TypeParsers.ANY_SET, targetType);
}
if(Map.class.isAssignableFrom(rawType)){
return invokeTypeParser(preprocessedInput, TypeParsers.ANY_MAP, targetType);
}
}
if(targetType instanceof Class){
Class<?> cls = (Class<?>) targetType;
if(cls.isArray()){
return invokeTypeParser(preprocessedInput, TypeParsers.ANY_ARRAY, targetType);
}
if(containsStaticMethodNamedValueOf(cls)){
return invokeTypeParser(preprocessedInput, TypeParsers.ANY_CLASS_WITH_STATIC_VALUEOF_METHOD, targetType);
}
}
if(targetType instanceof GenericArrayType){
return invokeTypeParser(preprocessedInput, TypeParsers.ANY_ARRAY, targetType);
}
String message = "There is either no registered 'TypeParser' for that type, or that "
+ "type does not contain the following static factory method: '%s.%s(String)'.";
message = String.format(message, targetType, STATIC_FACTORY_METHOD_NAME);
message = makeParseErrorMsg(preprocessedInput, message, targetType);
throw new IllegalArgumentException(message);
}
private String preProcessInputString(String input, Type targetType) {
try {
return inputPreprocessor.prepare(input, new InputPreprocessorHelper(targetType));
} catch (Exception e) {
String message = "Exception thrown from InputPreprocessor: %s [%s] with message: "
+ "%s. See underlying exception for more information.";
message = String.format(message,
inputPreprocessor, inputPreprocessor.getClass(), e.getMessage());
message = makeParseErrorMsg(input, message, targetType);
throw new IllegalArgumentException(message, e);
}
}
private Object invokeTypeParser(String input, Type key, Type targetType) {
try {
TypeParser<?> typeParser = typeParsers.get(key);
ParseHelper parseHelper = new ParseHelper(this, targetType);
return typeParser.parse(input, parseHelper);
} catch (NumberFormatException e) {
String message = String.format("Number format exception %s.", e.getMessage());
message = makeParseErrorMsg(input, message, targetType);
throw new IllegalArgumentException(message, e);
} catch (RuntimeException e) {
String message = makeParseErrorMsg(input, e.getMessage(),targetType);
throw new IllegalArgumentException(message, e);
}
}
private boolean isPrimitive(Type targetType) {
if(targetType instanceof Class){
Class<?> c = (Class<?>) targetType;
return c.isPrimitive();
}
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.