51 lines
1.4 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// interfaces/interfaceprocessor/StringProcessor.java
2016-12-30 17:23:13 -08:00
// (c)2017 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.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-07-28 12:48:23 -06:00
// {java interfaces.interfaceprocessor.StringProcessor}
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
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 {
2016-01-25 18:05:55 -08:00
@Override // Covariant return:
public String process(Object input) {
2015-06-15 17:47:35 -07:00
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
*/