Updated Abstractions.
This commit is contained in:
@@ -3,15 +3,33 @@ package abstraction;
|
|||||||
import tester.Tester;
|
import tester.Tester;
|
||||||
|
|
||||||
class Examples {
|
class Examples {
|
||||||
ILoStrings someWords = new ConsString("feldspar", new ConsString("assignment", new ConsString("curb", new LastString("carpet"))));
|
|
||||||
|
ILoStrings someWords = new ConsString(
|
||||||
|
"feldspar",
|
||||||
|
new ConsString(
|
||||||
|
"assignment",
|
||||||
|
new ConsString("curb", new LastString("carpet"))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
boolean tests(Tester t) {
|
boolean tests(Tester t) {
|
||||||
return t.checkExpect(someWords.earliest(new OrderByShortness()), "curb")
|
return (
|
||||||
&& t.checkExpect(someWords.earliest(new OrderByAlpha()), "assignment")
|
t.checkExpect(someWords.earliest(new OrderByShortness()), "curb") &&
|
||||||
&& t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar")
|
t.checkExpect(
|
||||||
&& t.checkExpect(someWords.earliest(new OrderByLongness()), "assignment")
|
someWords.earliest(new OrderByAlpha()),
|
||||||
&& t.checkExpect(someWords.earliest(new OrderByUnalpha()), "feldspar")
|
"assignment"
|
||||||
&& t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet");
|
) &&
|
||||||
|
t.checkExpect(someWords.earliest(new OrderByOrder()), "feldspar") &&
|
||||||
|
t.checkExpect(
|
||||||
|
someWords.earliest(new OrderByLongness()),
|
||||||
|
"assignment"
|
||||||
|
) &&
|
||||||
|
t.checkExpect(
|
||||||
|
someWords.earliest(new OrderByUnalpha()),
|
||||||
|
"feldspar"
|
||||||
|
) &&
|
||||||
|
t.checkExpect(someWords.earliest(new OrderByDisorder()), "carpet")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,74 +38,112 @@ interface IStringsBefore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Negates an ordering.
|
// Negates an ordering.
|
||||||
class Disorder {
|
class Disorder implements IStringsBefore {
|
||||||
|
|
||||||
IStringsBefore ordering;
|
IStringsBefore ordering;
|
||||||
|
|
||||||
Disorder(IStringsBefore ordering) { this.ordering = ordering; }
|
Disorder(IStringsBefore ordering) {
|
||||||
|
this.ordering = ordering;
|
||||||
boolean after(String s1, String s2) {
|
}
|
||||||
return ! this.ordering.before(s1, s2);
|
|
||||||
|
public boolean before(String s1, String s2) {
|
||||||
|
return !this.ordering.before(s1, s2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class OrderByShortness implements IStringsBefore { public boolean before(String s1, String s2) {
|
class OrderByShortness implements IStringsBefore {
|
||||||
return s1.length() <= s2.length();
|
|
||||||
}}
|
|
||||||
|
|
||||||
class OrderByAlpha implements IStringsBefore { public boolean before(String s1, String s2) {
|
public boolean before(String s1, String s2) {
|
||||||
return s1.compareTo(s2) <= 0;
|
return s1.length() <= s2.length();
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class OrderByOrder implements IStringsBefore { public boolean before(String s1, String s2) {
|
class OrderByAlpha implements IStringsBefore {
|
||||||
return true;
|
|
||||||
}}
|
|
||||||
|
|
||||||
class OrderByLongness implements IStringsBefore { public boolean before(String s1, String s2) {
|
public boolean before(String s1, String s2) {
|
||||||
return new Disorder(new OrderByShortness()).after(s1, s2);
|
return s1.compareTo(s2) <= 0;
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class OrderByUnalpha implements IStringsBefore { public boolean before(String s1, String s2) {
|
class OrderByOrder implements IStringsBefore {
|
||||||
return new Disorder(new OrderByAlpha()).after(s1, s2);
|
|
||||||
}}
|
|
||||||
|
|
||||||
class OrderByDisorder implements IStringsBefore { public boolean before(String s1, String s2) {
|
public boolean before(String s1, String s2) {
|
||||||
return new Disorder(new OrderByOrder()).after(s1, s2);
|
return true;
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class OrderByLongness implements IStringsBefore {
|
||||||
|
|
||||||
|
public boolean before(String s1, String s2) {
|
||||||
|
return new Disorder(new OrderByShortness()).before(s1, s2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class OrderByUnalpha implements IStringsBefore {
|
||||||
|
|
||||||
|
public boolean before(String s1, String s2) {
|
||||||
|
return new Disorder(new OrderByAlpha()).before(s1, s2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class OrderByDisorder implements IStringsBefore {
|
||||||
|
|
||||||
|
public boolean before(String s1, String s2) {
|
||||||
|
return new Disorder(new OrderByOrder()).before(s1, s2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// A non-empty list of strings.
|
// A non-empty list of strings.
|
||||||
interface ILoStrings {
|
interface ILoStrings {
|
||||||
String val(); // Gets the value.
|
String val(); // Gets the value.
|
||||||
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
|
String earliest(IStringsBefore ordering); // Gets the earliest in a list of strings.
|
||||||
String earliestHelper(IStringsBefore ordering, String prev);
|
String earliestHelper(IStringsBefore ordering, String prev);
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class ALoStrings implements ILoStrings {
|
abstract class ALoStrings implements ILoStrings {
|
||||||
|
|
||||||
String val;
|
String val;
|
||||||
|
|
||||||
ALoStrings(String val) { this.val = val; }
|
ALoStrings(String val) {
|
||||||
|
this.val = val;
|
||||||
public String val() { return this.val; }
|
}
|
||||||
|
|
||||||
|
public String val() {
|
||||||
|
return this.val;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ConsString extends ALoStrings {
|
class ConsString extends ALoStrings {
|
||||||
|
|
||||||
ILoStrings nxt;
|
ILoStrings nxt;
|
||||||
|
|
||||||
ConsString(String val, ILoStrings nxt) {
|
ConsString(String val, ILoStrings nxt) {
|
||||||
super(val);
|
super(val);
|
||||||
this.nxt = nxt;
|
this.nxt = nxt;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String earliest(IStringsBefore ordering) {
|
public String earliest(IStringsBefore ordering) {
|
||||||
return ordering.before(this.val, this.nxt.val()) ? this.nxt.earliestHelper(ordering, this.val) : this.nxt.earliest(ordering);
|
return ordering.before(this.val, this.nxt.val())
|
||||||
|
? this.nxt.earliestHelper(ordering, this.val)
|
||||||
|
: this.nxt.earliest(ordering);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String earliestHelper(IStringsBefore ordering, String prev) {
|
public String earliestHelper(IStringsBefore ordering, String prev) {
|
||||||
return new ConsString(prev, this.nxt).earliest(ordering);
|
return new ConsString(prev, this.nxt).earliest(ordering);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class LastString extends ALoStrings {
|
class LastString extends ALoStrings {
|
||||||
LastString(String val) { super(val); }
|
|
||||||
public String earliest(IStringsBefore ordering) { return this.val; }
|
LastString(String val) {
|
||||||
public String earliestHelper(IStringsBefore ordering, String prev) { return prev; }
|
super(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String earliest(IStringsBefore ordering) {
|
||||||
|
return this.val;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String earliestHelper(IStringsBefore ordering, String prev) {
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user