Skip to content

Commit

Permalink
Convert OperationName to an extensible enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Oct 12, 2017
1 parent 0475f63 commit fc7beb0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ private static void statsDataset(JsonBuilder builder, DataAccessPoint access) {
builder.startObject() ;

operationCounters(builder, endpoint);
builder.key(JsonConst.operation).value(operName.name()) ;
builder.key(JsonConst.description).value(operName.getDescription()) ;
builder.key(JsonConst.operation).value(operName.getName()) ;
builder.key(JsonConst.description).value(operName.getName()) ;

builder.finishObject() ;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public static void describe(JsonBuilder builder, DataAccessPoint access) {
private static void describe(JsonBuilder builder, OperationName opName, List<Endpoint> endpoints) {
builder.startObject() ;

builder.key(JsonConst.srvType).value(opName.name()) ;
builder.key(JsonConst.srvDescription).value(opName.getDescription()) ;
builder.key(JsonConst.srvType).value(opName.getName()) ;
builder.key(JsonConst.srvDescription).value(opName.getName()) ;

builder.key(JsonConst.srvEndpoints) ;
builder.startArray() ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,68 @@

package org.apache.jena.fuseki.server;

public enum OperationName {
// Fixed names give the codebase some resilience.
Query("SPARQL Query"),
Update("SPARQL Update"),
Upload("File Upload"),
GSP_RW("Graph Store Protocol"),
GSP_R("Graph Store Protocol (Read)"),
Quads_RW("HTTP Quads"),
Quads_R("HTTP Quads (Read)")
;

private final String description ;
private OperationName(String description) { this.description = description ; }
public String getDescription() { return description ; }
import java.util.HashMap;
import java.util.Map;

/**
* Names (symbols) for operations.
* An {@code OperationName} is not related to the service name used to invoke the operation.
* That is determined by the {@link Endpoint}.
*/
public class OperationName {

// Create intern'ed symbols.
static private Map<String, OperationName> registered = new HashMap<>();

/**
* Create an intern'ed {@code OperationName}. That is, if the object has already been
* created, return the original. There is only ever one object for a given name.
* (It is an extensible enum without subclassing).
*/
static public OperationName register(String name) {
return registered.computeIfAbsent(name, (n)->new OperationName(n));
}

public static OperationName Query = register("SPARQL Query");
public static OperationName Update = register("SPARQL Update");
public static OperationName Upload = register("File Upload");
public static OperationName GSP_RW = register("Graph Store Protocol");
public static OperationName GSP_R = register("Graph Store Protocol (Read)");
public static OperationName Quads_RW = register("HTTP Quads");
public static OperationName Quads_R = register("HTTP Quads (Read)");

private final String name ;
private OperationName(String name) { this.name = name ; }

public String getName() { return name ; }

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}

// Could be this == obj
// because we intern'ed the object

@Override
public boolean equals(Object obj) {
if ( this == obj )
return true;
if ( obj == null )
return false;
if ( getClass() != obj.getClass() )
return false;
OperationName other = (OperationName)obj;
if ( name == null ) {
if ( other.name != null )
return false;
} else if ( !name.equals(other.name) )
return false;
return true;
}

}

0 comments on commit fc7beb0

Please sign in to comment.