Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // CHALLENGE (work in pairs):
- // Move from f(o) -> o.f() and make sure that the code is CQS compliant.
- //
- // 1
- //class CsvParser<T extends Line> {
- // Collection<T> parse(File location) {
- // }
- //}
- class Csv<T extends Line> {
- private final File location;
- Csv(File location){
- this.location = location;
- }
- Collection<T> lines(){
- }
- }
- // 2
- //interface Pinger {
- // void sendPing();
- //}
- interface Ping {
- void send();
- }
- // 3
- //interface TcpSocket {
- // Stream<Byte> startReading();
- //}
- interface Content {
- Stream<Byte> bytes();
- String text();
- }
- interface TcpSocket {
- Content content();
- }
- // 4
- // Make me CQS friendly!
- //interface Input {
- // boolean validate();
- //}
- interface Input {
- void validate();
- boolean isValid();
- }
- // 5
- // Iovation - a company that maintains a blacklist of email addresses and corresponding IPs
- //interface IovationService {
- // boolean shouldBlockWebsiteVisitor(IovationClientRequest request);
- //}
- //interface IovationClientRequest {
- // String getEmail();
- // String getIpAddress();
- //}
- class IovationClient {
- private final IovationClientRequest request;
- IovationClient(IovationClientRequest request){
- this.request = request;
- }
- boolean blocked();
- }
- interface IovationClientRequest {
- String email();
- String ip();
- }
- // 6
- //interface AnonymousUserAuthenticator {
- // Token authenticate(String username, String password) throws WrongAuthenticationCredentialsProvided;
- //}
- interface AnonymousUser {
- private final String username;
- private final String password;
- AnonymousUser(String username, String password){
- this.username = username;
- this.password = password;
- }
- void authenticate() throws WrongAuthenticationCredentialsProvided;
- AuthenticatedUser authenticatedUser();
- }
- interface AuthenticatedUser {
- Token token();
- }
- // 7
- //interface Suite {
- // interface SuiteTest {
- // void print();
- // boolean successful()
- // }
- // void runAndWait();
- // Collection<SuiteTest> listSuiteTests();
- //}
- //suite.runAndWait()
- //for (SuiteTest suiteTest : suite.listSuiteTests()) {
- // if (!suiteTest.successful() ) {
- // // pretty printing
- // suiteTest.print();
- // }
- //}
- interface TestsSuite {
- interface Test {
- void printPretty();
- boolean successful();
- boolean unsuccessful();
- }
- void run();
- Collection<Test> tests();
- }
- testsSuite.run()
- testsSuite.tests()
- .stream()
- .filter(Test::unsuccessful)
- .forEach(Test::printPretty);
- // 8
- // Booleans are queries, but exception to the CQR rule naming. They are not nouns. Let's fix it (guess how).
- enum Severity {
- boolean major();
- }
- class Specification {
- boolean satisfiedBy(Entity entity);
- }
- // 9
- //boolean destroyButtonAvailable =
- // widgets
- // .stream()
- // .filter(Widget::isButton)
- // .filter(button -> button.label().equals("Destroy The World"))
- // .isPresent();
- boolean destroyButtonAvailable =
- widgets
- .stream()
- .filter(Widget::isButton)
- .map(Widget::label)
- .anyMatch(label -> label.equals("Destroy The World"));
- // 10 transform a full name into a twitter handle. Jack Ma must become @jack.ma
- class TwitterName{
- TwitterName(String fullname){
- }
- String toString(){
- return @ + fullname;
- }
- }
- String fullName = "Jack Ma";
- ...
- String twitterHandle =
Add Comment
Please, Sign In to add comment