2015-09-07 11:44:36 -06:00
|
|
|
|
// interfaces/interfaceprocessor/StringProcessor.java
|
2015-11-14 16:18:05 -08:00
|
|
|
|
// <20>2016 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
|
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
|
package interfaces.interfaceprocessor;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
2015-12-02 09:20:27 -08:00
|
|
|
|
interface StringProcessor extends Processor {
|
2015-06-15 17:47:35 -07:00
|
|
|
|
@Override
|
2015-12-02 09:20:27 -08:00
|
|
|
|
String process(Object input); // (1)
|
|
|
|
|
String s = // (2)
|
|
|
|
|
"If she weighs the same as a duck, she's made of wood";
|
|
|
|
|
static void main(String[] args) { // (3)
|
|
|
|
|
Applicator.apply(new Upcase(), s);
|
|
|
|
|
Applicator.apply(new Downcase(), s);
|
|
|
|
|
Applicator.apply(new Splitter(), s);
|
2015-06-15 17:47:35 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 09:20:27 -08:00
|
|
|
|
class Upcase implements StringProcessor {
|
2015-06-15 17:47:35 -07:00
|
|
|
|
@Override
|
|
|
|
|
public String process(Object input) { // Covariant return
|
|
|
|
|
return ((String)input).toUpperCase();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 09:20:27 -08:00
|
|
|
|
class Downcase implements StringProcessor {
|
2015-06-15 17:47:35 -07:00
|
|
|
|
@Override
|
|
|
|
|
public String process(Object input) {
|
|
|
|
|
return ((String)input).toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 09:20:27 -08:00
|
|
|
|
class Splitter implements StringProcessor {
|
2015-06-15 17:47:35 -07:00
|
|
|
|
@Override
|
|
|
|
|
public String process(Object input) {
|
|
|
|
|
return Arrays.toString(((String)input).split(" "));
|
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
|
}
|
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
|
Using Processor Upcase
|
|
|
|
|
IF SHE WEIGHS THE SAME AS A DUCK, SHE'S MADE OF WOOD
|
|
|
|
|
Using Processor Downcase
|
|
|
|
|
if she weighs the same as a duck, she's made of wood
|
|
|
|
|
Using Processor Splitter
|
|
|
|
|
[If, she, weighs, the, same, as, a, duck,, she's, made, of,
|
|
|
|
|
wood]
|
2015-09-07 11:44:36 -06:00
|
|
|
|
*/
|