I have two grammar files/visitors, Simple and Complex, that parse JSON objects into strings. The Complex objects that I'm parsing can essentially contain a number of Simple objects (along with additional things). For simplicity's sake let's say that when I parse a base Simple object (not a Simple object contained within the Complex object) I want to start the string with something like "Simple start: ", but when I reach a Simple object within a Complex object I want to start it with something else, say "Simple within Complex: ".
So currently I have two different visitor classes, the Simple visitor's visitSimpleObject method will return the String starting with "Simple Start: " whereas the Complex visitor's visitSimpleObject method will return the String starting with "Simple within Complex: ". Besides this difference everything else should be the same, everything else within a Simple object can be parsed the same whether it is on it's own or inside a Complex object.
My question is, how can I share code between these two visitors? Obviously I could copy and paste all the applicable SimpleVisitor code into the ComplexVisitor but then I'll have to keep them in sync for any changes.
Note: The two visitor classes already extend a BaseVisitor class so I can't use typical inheritance
You can use inheritance to do this, see for example:
https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
A short example would be something like:
abstract class Visitor{
public void sharedMethod() {
//Do something
}
public abstract void visitSimpleObject();
}
class SimpleVisitor extends Visitor{
#Override
public void visitSimpleObject() {
System.out.println("Simple Start:");
}
}
class ComplexVisitor extends Visitor{
#Override
public void visitSimpleObject() {
System.out.println("Simple within Complex:");
}
}
In this case both your visitors would extend from the same superclass (Visitor) where the shared code is. Both subclasses can define their own behavior. The superclass can itself also extend another kind of visitor (or implement an interface).
EDIT
After the comments maybe more like this:
Sample Ex.g4:
grammar Ex;
START_COMPLEX : 'complex';
START_SIMPLE : 'simple';
SEPERATOR : ':';
TEXT : [A-Za-z]+;
simple : START_SIMPLE ' ' SEPERATOR ' ' TEXT;
complex : START_COMPLEX ' ' SEPERATOR ' ' TEXT;
And a code sample:
public class Example{
abstract class Visitor extends ExBaseVisitor<String>{
#Override
public String visitComplex(ExParser.ComplexContext ctx) {
System.out.println("Visiting complex");
return "";
}
}
class SimpleVisitor extends Visitor{
#Override
public String visitSimple(ExParser.SimpleContext ctx) {
System.out.println("Visiting Simple! " + ctx.TEXT());
return "";
}
}
class ComplexVisitor extends Visitor{
#Override
public String visitSimple(ExParser.SimpleContext ctx) {
System.out.println("Visiting Simple, from within complex! " + ctx.TEXT());
return "";
}
}
public static void main(String[] args) {
String text = "simple : hi";
CharStream charStream = new ANTLRInputStream(text);
ExLexer exLexer = new ExLexer(charStream);
TokenStream tokenStream = new CommonTokenStream(exLexer);
ExParser exParser = new ExParser(tokenStream);
ComplexVisitor complexVisitor = new Example().new ComplexVisitor();
complexVisitor.visit(exParser.simple());
String text2 = "simple : hmmmmm";
CharStream charStream2 = new ANTLRInputStream(text2);
ExLexer exLexer2 = new ExLexer(charStream2);
TokenStream tokenStream2 = new CommonTokenStream(exLexer2);
ExParser exParser2 = new ExParser(tokenStream2);
SimpleVisitor simpleVisitor = new Example().new SimpleVisitor();
simpleVisitor.visit(exParser2.simple());
}
}
For me this prints:
Visiting Simple, from within complex! hi
Visiting Simple! hmmmmm
The shared code is now visitComplex which is maybe a bit silly but for the example maybe ok.
Related
I want to use Stream API to keep track of a variable while changing it with functions.
My code:
public String encoder(String texteClair) {
for (Crypteur crypteur : algo) {
texteClair = crypteur.encoder(texteClair);
}
return texteClair;
}
I have a list of classes that have methods and I want to put a variable inside all of them, like done in the code above.
It works perfectly, but I was wondering how it could be done with streams?
Could we use reduce()?
Use an AtomicReference, which is effectively final, but its wrapped value may change:
public String encoder(String texteClair) {
AtomicReference<String> ref = new AtomicReference<>(texteClair);
algo.stream().forEach(c -> ref.updateAndGet(c::encoder)); // credit Ole V.V
return ref.get();
}
Could we use reduce()?
I guess we could. But keep in mind that it's not the best case to use streams.
Because you've mentioned "classes" in plural, I assume that Crypteur is either an abstract class or an interface. As a general rule you should favor interfaces over abstract classes, so I'll assume the that Crypteur is an interface (if it's not, that's not a big issue) and it has at least one implementation similar to this :
public interface Encoder {
String encoder(String str);
}
public class Crypteur implements Encoder {
private UnaryOperator<String> operator;
public Crypteur(UnaryOperator<String> operator) {
this.operator = operator;
}
#Override
public String encoder(String str) {
return operator.apply(str);
}
}
Then you can utilize your encoders with stream like this:
public static void main(String[] args) {
List<Crypteur> algo =
List.of(new Crypteur(str -> str.replaceAll("\\p{Punct}|\\p{Space}", "")),
new Crypteur(str -> str.toUpperCase(Locale.ROOT)),
new Crypteur(str -> str.replace('A', 'W')));
String result = encode(algo, "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system");
System.out.println(result);
}
public static String encode(Collection<Crypteur> algo, String str) {
return algo.stream()
.reduce(str,
(String result, Crypteur encoder) -> encoder.encoder(result),
(result1, result2) -> { throw new UnsupportedOperationException(); });
}
Note that combiner, which is used in parallel to combine partial results, deliberately throws an exception to indicate that this task ins't parallelizable. All transformations must be applied sequentially, we can't, for instance, apply some encoders on the given string and then apply the rest of them separately on the given string and merge the two results - it's not possible.
Output
EVERYPIECEOFKNOWLEDGEMUSTHWVEWSINGLEUNWMBIGUOUSWUTHORITWTIVEREPRESENTWTIONWITHINWSYSTEM
This has to do with inspecting lists of complex objects, viewing one property only.
In Visual studio, C# with LINQ I can do this during debugging in the Watch:
list.Select(x => x.PropOfInterest).ToList()
A colleague's suggestion was doing something like:
getList().stream().map(x -> x.getInterestingProperty())
But that doesn't work in the Expressions in Eclipse, because "Lambda expressions cannot be used in an evaluation expression".
What's the best way to inspect a generic list like that during debugging in Java?
Great question. Here is what you can do.
Implement a small class that implements java.util.function.Function<T, String> whose apply() method returns the property you want. Something like this:
private static class F implements Function<Action, String>{
#Override
public String apply( Action t ){
return t.getName();
}
}
Then during debugging, use the following watch expression: getList().stream().map( new F() ).collect( java.util.stream.Collectors.toList() )
(You may try to make it general by using generics, I guess. I will be trying that anyhow on my system.)
Edit: Here's a generic version of the class using which one field of any class may be shown in the watch expression evaluation (assuming that you have a get*() method for it in the Java bean format.) In the watch expression mentioned earlier, instead of new F(), pass new F( "yourFieldName" ). Like this: getList().stream().map( new F( "propertyOfInterest" ) ).collect( java.util.stream.Collectors.toList() ).
public class F<T> implements Function<T, Object>{
private String fieldName;
private String getMethodName;
public F(String fieldName) {
super();
this.fieldName = fieldName;
this.getMethodName = "get" + fieldName.substring( 0, 1 ).toUpperCase() + fieldName.substring( 1 );
}
#Override
public Object apply( T t ){
try {
return t.getClass().getMethod( getMethodName ).invoke( t );
} catch (Exception e) {
return e.getClass().getSimpleName() + ": " + e.getMessage();
}
}
}
I got about 6 classes that are doing 'almost' the same with different values. I'll give two classes and an example below and then describe what i'm doing.
public class AttributeRangeRule implements Template {
#Override
public String writeTemplate(BusinessRule businessRule) throws Exception {
String link = TemplateReader.getInstance().getLinkToQuery(businessRule.getBusinessRuleTypeCode());
String template = TemplateReader.getInstance().readQuery(link);
ST templateFixer = new ST(template);
templateFixer.add("code", businessRule.getBusinessRuleTypeCode());
templateFixer.add("attribute_table", businessRule.getListOfTables().get(0).getName());
templateFixer.add("operator", businessRule.getOperator().getName());
templateFixer.add("range_min", businessRule.getListOfValues().get(0).getValue());
templateFixer.add("range_max", businessRule.getListOfValues().get(1).getValue());
templateFixer.add("attribute_column", businessRule.getListOfColumns().get(0).getName());
templateFixer.add("error", businessRule.getErrorMessage());
templateFixer.add("GreaterOrEqual", ">=");
templateFixer.add("LessOrEqual", "<=");
templateFixer.add("LessThen", "<");
templateFixer.add("GreaterThen", ">");
String templateDLL = templateFixer.render();
return templateDLL;
}
}
public class AttributeCompareRule implements Template {
#Override
public String writeTemplate(BusinessRule businessRule) throws Exception {
String link = TemplateReader.getInstance().getLinkToQuery(businessRule.getBusinessRuleTypeCode());
String template = TemplateReader.getInstance().readQuery(link);
ST templateFixer = new ST(template);
templateFixer.add("code", businessRule.getBusinessRuleTypeCode());
templateFixer.add("attribute_table", businessRule.getListOfTables().get(0).getName());
templateFixer.add("operand", businessRule.getOperator().getName());
templateFixer.add("compare_with", businessRule.getListOfValues().get(0).getValue());
templateFixer.add("error", businessRule.getErrorMessage());
String templateDLL = templateFixer.render();
return templateDLL;
}
}
templateFixer.add("code..") is for example duplicate. They are written the same in both classes but the value is different.
I have different classes with different implementation of the method writeTemplate(). As you can see AttributeRangeRule is different from AttributeCompareRule. This code is writting a query for me. Intellij is telling me that the code is duplicate even if the values arent unique. I have no idea how to solve this issue. How can i solve this issue, since duplicate code isnt the best to have in your code. Thanks in advance.
You should try to take advantage of OOPS concepts here and use inheritance here
You can create a base class named AttributeRule that overrides writeTemplate() method and put all the redundant code in there and have this class be extended by your subclasses i.e. AttributeCompareRule and AttributeCompareRule
Here's the conceptual idea and some snippets
public class AttributeRule implements Template {
#Override
public String writeTemplate(BusinessRule businessRule) throws Exception {
String link = TemplateReader.getInstance().getLinkToQuery(businessRule.getBusinessRuleTypeCode());
String template = TemplateReader.getInstance().readQuery(link);
ST templateFixer = new ST(template);
templateFixer.add("code", businessRule.getBusinessRuleTypeCode());
templateFixer.add("attribute_table", businessRule.getListOfTables().get(0).getName());
}
}
public class AttributeCompareRule extends AttributeRule {
#Override
public String writeTemplate(BusinessRule businessRule) throws Exception {
super.writeTemplate(rule);
// Custom class code here
templateFixer.add("operand", businessRule.getOperator().getName());
templateFixer.add("compare_with", businessRule.getListOfValues().get(0).getValue());
templateFixer.add("error", businessRule.getErrorMessage());
String templateDLL = templateFixer.render();
return templateDLL;
}
}
public class AttributeRangeRule extends AttributeRule {
super.writeTemplate(rule);
// Custom class code here
}
A user enters a code and the type of that code is determined by regular expressions. There are many different type of codes, such as EAN, ISBN, ISSN and so on. After the type is detected, a custom query has to be created for the code. I thought it might be a good idea to create a strategy for type, but with time it feels wrong.
public interface SearchQueryStrategie {
SearchQuery createSearchQuery(String code);
}
-
public class IssnSearchQueryStrategie implements SearchQueryStrategie {
#Override
public SearchQuery createSearchQuery(final String code) {
// Create search query for issn number
}
}
-
public class IsbnSearchQueryStrategie implements SearchQueryStrategie {
#Override
public SearchQuery createSearchQuery(final String code) {
// Create search query for ISBN number
}
}
-
public class EanShortNumberSearchQueryStrategie implements SearchQueryStrategie {
#Override
public SearchQuery createSearchQuery(final String code) {
// Create search query for ean short number
}
}
-
public class TestApplication {
public static void main(final String... args) {
final String code = "1144875X";
SearchQueryStrategie searchQueryStrategie = null;
if (isIssn(code)) {
searchQueryStrategie = new IssnSearchQueryStrategie();
} else if (isIsbn(code)) {
searchQueryStrategie = new IsbnSearchQueryStrategie();
} else if (isEan(code)) {
searchQueryStrategie = new EanShortNumberSearchQueryStrategie();
}
if (searchQueryStrategie != null) {
performSearch(searchQueryStrategie.createSearchQuery(code));
}
}
private SearchResult performSearch(final SearchQuery searchQuery) {
// perform search
}
// ...
}
I have to say that there are many more strategies. How should I dispatch the code to the right strategy?
My second approach was to put a boolean method into every strategy to decide if the code is correct for that strategy.
public class TestApplication {
final SearchQueryStrategie[] searchQueryStrategies = {new IssnSearchQueryStrategie(), new IsbnSearchQueryStrategie(),
new EanShortNumberSearchQueryStrategie()};
public static void main(final String... args) {
final String code = "1144875X";
for (final SearchQueryStrategie searchQueryStrategie : searchQueryStrategie) {
if (searchQueryStrategie.isRightCode(code)) {
searchQueryStrategie.createSearchQuery(code);
break;
}
}
}
private SearchResult performSearch(final SearchQuery searchQuery) {
// perform search
}
// ...
}
How would you solve this problem? Is the strategy pattern the right one for my purposes?
If you are using Java 8 and you can profit from the functional features I think one Enum will be sufficient.
You can avoid using if/else statements by mapping each type of code with a Function that will return the query that needs to be executed:
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Pattern;
public enum CodeType
{
EAN("1|2|3"),
ISBN("4|5|6"),
ISSN("7|8|9");
String regex;
Pattern pattern;
CodeType(String regex)
{
this.regex = regex;
this.pattern = Pattern.compile(regex);
}
private static Map<CodeType, Function<String, String>> QUERIES =
new HashMap<>();
static
{
QUERIES.put(EAN, (String code) -> String.format("Select %s from EAN", code));
QUERIES.put(ISBN, (String code) -> String.format("Select %s from ISBB", code));
QUERIES.put(ISSN, (String code) -> String.format("Select %s from ISSN", code));
}
private static CodeType evalType(String code)
{
for(CodeType codeType : CodeType.values())
{
if (codeType.pattern.matcher(code).matches())
return codeType;
}
// TODO DON'T FORGET ABOUT THIS NULL HERE
return null;
}
public static String getSelect(String code)
{
Function<String, String> function = QUERIES.get(evalType(code));
return function.apply(code);
}
}
And in the main you can test your query:
public class Main
{
public static void main(String... args)
{
System.out.println(CodeType.getSelect("1"));
// System.out: Select 1 from EAN
System.out.println(CodeType.getSelect("4"));
// System.out: Select 4 from ISBB
System.out.println(CodeType.getSelect("9"));
// System.out: Select 9 from ISSN
}
}
I usually tend to keep the code as compact as possible.
Some people dislike enums, so I believe you can use a normal class instead.
You can engineer further the way you obtain the QUERIES (selects), so instead of having String templates you can have a Runnable there.
If you don't want to use the the functional aspects of Java 8 you can use Strategy objects that are associated with each type of code:
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Pattern;
public enum CodeType2
{
EAN("1|2|3", new StrategyEAN()),
ISBN("4|5|6", new StrategyISBN()),
ISSN("7|8|9", new StrategyISSN());
String regex;
Pattern pattern;
Strategy strategy;
CodeType2(String regex, Strategy strategy)
{
this.regex = regex;
this.pattern = Pattern.compile(regex);
this.strategy = strategy;
}
private static CodeType2 evalType(String code)
{
for(CodeType2 codeType2 : CodeType2.values())
{
if (codeType2.pattern.matcher(code).matches())
return codeType2;
}
// TODO DON'T FORGET ABOUT THIS NULL HERE
return null;
}
public static void doQuery(String code)
{
evalType(code).strategy.doQuery(code);
}
}
interface Strategy { void doQuery(String code); }
class StrategyEAN implements Strategy {
#Override
public void doQuery(String code)
{
System.out.println("EAN-" + code);
}
}
class StrategyISBN implements Strategy
{
#Override
public void doQuery(String code)
{
System.out.println("ISBN-" + code);
}
}
class StrategyISSN implements Strategy
{
#Override
public void doQuery(String code)
{
System.out.println("ISSN-" + code);
}
}
And the main method will look like this:
public class Main
{
public static void main(String... args)
{
CodeType2.doQuery("1");
CodeType2.doQuery("4");
CodeType2.doQuery("9");
}
}
So, The strategy pattern is indeed the right choice here, but strategy by itself is not enough. You have several options:
Use a Factory with simple if/else or switch. It's ugly, error prone to extend with new strategies, but is simple and quick to implement.
Use a registry. During the application initialization phase you can register in a registry each SearchQueryStratgeyFactory with the right code. For instance if you use a simple Map you can just do :
strategyRegistry.put("isbn", new IsbnSearchStrategyFactory());
strategyRegistry.put("ean", new EanSearchStrategyFactory());
.... and so on
Then when you need to get the right strategy you just get() the strategy factory from the map using the code id. This approach is better if you have a lot of strategies, but it requires an aditional iitialization step during the application startup.
Use a service locator. ServiceLocator is a pattern that enables the dynamic lookup of implementations. Java comes with an implementation of the ServiceLocator pattern -> the infamous ServiceLoader class. This is my favourite approach because it allows for complete decoupling of the consumer and implementation. Also using the service locator you can easily add new strategies without having to modify the existing code. I won't explain how to use the ServiceLoader - there is plenty of information online. I'll just mention that using the service locator you'll need to implement a "can process such codes ?" logic in each strategy factory. For instance if the factory cannot create a strategy for "isbn" then return null and try with the next factory.
Also note that in all cases you work with factories that produce the strategy implementations.
PS: It's strategy not strategie :)
Your approach is not the Strategy Pattern. Strategy Pattern is all about customizing behavior of an object (Context in terms of this pattern) by passing alternative Strategy object to it. By this way, we don't need to modify the source code of the Context class but still can customize the behavior of objects instanced from it.
Your problem is somewhat related to the Chain of Responsibility (CoR) Pattern where you have a request (your code) and need to figure out which SearchQueryStrategie in a predefined list should handle the request.
The second approach -- using array -- that you mentioned is fine. However, to make it usable in production code, you must have another object -- let's say Manager -- that manages the array and is responsible to find the relevant element for each request. So your client code have to depend on two objects: the Manager and the result SearchQueryStrategie. As you can see, the source code of Manager class tend to be changed frequently because new implementations of SearchQueryStrategie may come. This might make your client annoyed.
That's why the CoR Pattern uses the linked list mechanism instead of array. Each SearchQueryStrategie object A would hold a reference to a next SearchQueryStrategie B. If A cannot handle the request, it will delegate to B (it can even decorate the request before delegating). Of course, somewhere still must know all kinds of strategies and create a linked list of SearchQueryStrategie, but your client will then depend only on a SearchQueryStrategie object (the head one of the list).
Here is the code example:
class SearchQueryConsumer {
public void consume(SearchQuery sq) {
// ...
}
}
abstract class SearchQueryHandler {
protected SearchQueryHandler next = null;
public void setNext(SearchQueryHandler next) { this.next = next; }
public abstract void handle(String code, SearchQueryConsumer consumer);
}
class IssnSearchQueryHandler extends SearchQueryHandler {
#Override
public void handle(String code, SearchQueryConsumer consumer) {
if (issn(code)) {
consumer.consume(/* create a SearchQuery */);
} else if (next != null) {
next.handle(code, consumer);
}
}
private boolean issn(String code) { ... }
}
What i recommend is using the Factory pattern. It describes and handles your scenario better.
Factory Pattern
You can design in the following way (using concepts of factory DP and polymorphism):
Code as interface.
ISSNCode, ISBNCode and EANCode as concrete classes
implementing Code interface, having single-arg constructor taking text as String.
Code has method getInstanceOfCodeType(String text) which returns an instance of a sub-class of Code (decided by checking the type of text passed to it). Let's say the returned value be code
Class SearchQueryStrategieFactory with
getSearchQueryStrategie(code) method. It consumes the returned value from step 3, and generates different
instances of SearchQueryStrategie subclasses based on code type using new operator and, then returns the same.
So, you need to call two methods getInstanceOfCodeType(text) and getSearchQueryStrategie(code) from anywhere.
Instead of implicitly implementing the factory inside main, keep the whole factory code separate, to make it easily maintainable and extensible .
In one of my projects I need to compare the URI with several regex patterns(15+ regex patterns). Currently I have used a if ladder to see if either one of them gets matched and there onward the logical part of the code is executed.
Glimpse of the code now:
if (uri.matches(Constants.GET_ALL_APIS_STORE_REGEX)) {
long lastUpdatedTime = InBoundETagManager.apisGet(null, null, tenantDomain, null);
String eTag = ETagGenerator.getETag(lastUpdatedTime);
if (eTag.equals(ifNoneMatch)) {
message.getExchange().put("ETag", eTag);
generate304NotModifiedResponse(message);
}
message.getExchange().put("ETag", eTag);
}
else if (uri.matches(Constants.GET_API_FOR_ID_REGEX)) { // /apis/{apiId}
apiId = UUIDList.get(0);
String requestedTenantDomain = RestApiUtil.getRequestedTenantDomain(tenantDomain);
long lastUpdatedTime = InBoundETagManager.apisApiIdGet(apiId, requestedTenantDomain, uri);
String eTag = ETagGenerator.getETag(lastUpdatedTime);
handleInterceptorResponse(message, ifNoneMatch, eTag);
}
else if (uri.matches(Constants.GET_SWAGGER_FOR_API_ID_REGEX)) { // /apis/{apiId}/swagger
apiId = UUIDList.get(0);
long lastUpdatedTime = InBoundETagManager.apisApiIdSwaggerGet(apiId, tenantDomain);
String eTag = ETagGenerator.getETag(lastUpdatedTime);
if (lastUpdatedTime == 0L) {
log.info("No last updated time available for the desired API swagger json file");
}
handleInterceptorResponse(message, ifNoneMatch, eTag);
}
Can someone please introduce me with a more neat and clever way of doing this regex matching thing?
One url-type(regex) = one handler = one class. This way would be much easier to read and support especially if you have 15 regex checks.
interface URLHandler {
void handle();
boolean isSupported(String url);
}
class GetAllApisStoreHandler implements URLHandler{
private static final Pattern GET_ALL_API_STORE_PATTERN = Pattern.compile(GET_ALL_APIS_STORE_REGEX);
public boolean isSupported(String url) {
return GET_ALL_API_STORE_PATTERN.matcher(url).matches();
}
public void handle(...) {
...
}
}
class GetApiIdHandler implements URLHandler{
private static final Pattern GET_API_ID_REGEX = Pattern.compile(GET_API_ID_REGEX);
public boolean isSupported(String url) {
return GET_API_ID_PATTERN.matcher(url).matches();
}
public void handle(...) {
...
}
}
class GetApiIdHandler implements URLHandler{
private static final Pattern GET_SWAGGER_FORAPI_ID_PATTERN = Pattern.compile(GET_SWAGGER_FOR_API_ID_REGEX);
public boolean isSupported(String url) {
return GET_SWAGGER_FORAPI_ID_PATTERN.matcher(url).matches();
}
public void handle(...) {
...
}
}
class Main {
private List<URLHandler> urlHandlers;
public void method(){
...
for (URLHandler handler : urlHandlers) {
if(handler.isSupported(url)) {
handler.handle(arg1, arg2, arg3, ...);
}
}
...
}
}
Using multiple classes as #KonstantinLabun proposed is probably the way to go(*), but it shouldn't lead to much code duplication. So use an abstract class instead of (or in addition to an interface). Or (mis)use default methods.
abstract class URLHandler {
abstract void handle();
abstract Pattern urlPattern():
final boolean isSupported(String url) {
return urlPattern().matcher(url).matches();
}
}
class GetAllApisStoreHandler extends URLHandler{
private static final Pattern URL_PATTERN =
Pattern.compile(Constants.GET_ALL_APIS_STORE_REGEX);
Pattern urlPattern() {
return URL_PATTERN;
}
public void handle(...) {
...
}
}
There's no need to invent names for the PATTERN as its scope identified it already. The static field exists only as an optimization, so that the Pattern don't get compiled for each match.
(*) There's nothing wrong with a single class, as long as it's concise (I like spaghetti except in code) and doesn't leak implementation details. There's nothing wrong with multiple classes (except maybe on Android as 50 kB per class might matter) as long as they don't lead to code bloat. An enum is sometimes a good solution, too.
Explanation of abstract class vs. interface
An interface forces you to implement its methods(**), which may quickly lead to duplication. It's advantage is multiple inheritance and some conceptual purity.
An abstract class allows you to gather the common parts. But there's no dilemma, you can do both, see e.g., interface List and abstract class AbstractList.
(**) Since Java 8, an interface can have default methods, so this is no more true. Assuming you want to use them for this purpose. It can't declare any state, but it can access the state of the object. For example, my above URLHandler could be such an interface. There are still disadvantages, e.g., methods must be public and mustn't be final.