2015-09-07 11:44:36 -06:00
|
|
|
// interfaces/interfaceprocessor/StringProcessor.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 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
|
2021-01-31 15:42:31 -07:00
|
|
|
String process(Object input); // [1]
|
|
|
|
String S = // [2]
|
2017-05-10 11:45:39 -06:00
|
|
|
"If she weighs the same as a duck, " +
|
|
|
|
"she's made of wood";
|
2021-01-31 15:42:31 -07:00
|
|
|
static void main(String[] args) { // [3]
|
2016-11-21 12:37:57 -08:00
|
|
|
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
|
2017-05-10 11:45:39 -06:00
|
|
|
[If, she, weighs, the, same, as, a, duck,, she's, made,
|
|
|
|
of, wood]
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|