Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add object filter support #69

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions examples/src/main/java/com/softlayer/api/example/ListServers.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.softlayer.api.ApiClient;
import com.softlayer.api.service.Account;
import com.softlayer.api.service.Hardware;
import com.softlayer.api.service.software.Description;
import com.softlayer.api.service.virtual.Guest;

/** List all physical and virtual servers on an account */
Expand All @@ -13,23 +14,23 @@ public void run(ApiClient client) throws Exception {
Account.Service service = Account.service(client);

// To get specific information on an account (servers in this case) a mask is provided
service.withMask().hardware().
fullyQualifiedDomainName().
primaryIpAddress().
primaryBackendIpAddress();
service.withMask().hardware().operatingSystem().softwareLicense().softwareDescription().
manufacturer().
name().
version();
service.withMask().virtualGuests().
fullyQualifiedDomainName().
primaryIpAddress().
primaryBackendIpAddress();
service.withMask().virtualGuests().operatingSystem().softwareLicense().softwareDescription().
manufacturer().
name().
version();
Hardware.Mask hardwareMask = service.withMask().hardware();
hardwareMask.fullyQualifiedDomainName();
hardwareMask.primaryIpAddress();
hardwareMask.primaryBackendIpAddress();
Description.Mask descriptionMask = hardwareMask.operatingSystem().softwareLicense().softwareDescription();
descriptionMask.manufacturer();
descriptionMask.name();
descriptionMask.version();
Guest.Mask guestMask = service.withMask().virtualGuests();
guestMask.fullyQualifiedDomainName();
guestMask.primaryIpAddress();
guestMask.primaryBackendIpAddress();
descriptionMask = guestMask.operatingSystem().softwareLicense().softwareDescription();
descriptionMask.manufacturer();
descriptionMask.name();
descriptionMask.version();

// Calling getObject will now use the mask
Account account = service.getObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.softlayer.api.ResponseHandler;
import com.softlayer.api.service.Account;
import com.softlayer.api.service.Hardware;
import com.softlayer.api.service.software.Description;
import com.softlayer.api.service.virtual.Guest;

/** Asynchronous version of {@link ListServers} */
Expand All @@ -14,22 +15,22 @@ public void run(ApiClient client) throws Exception {
Account.Service service = Account.service(client);

// To get specific information on an account (servers in this case) a mask is provided
service.withMask().hardware().
fullyQualifiedDomainName().
primaryIpAddress().
primaryBackendIpAddress();
service.withMask().hardware().operatingSystem().softwareLicense().softwareDescription().
manufacturer().
name().
version();
service.withMask().virtualGuests().
fullyQualifiedDomainName().
primaryIpAddress().
primaryBackendIpAddress();
service.withMask().virtualGuests().operatingSystem().softwareLicense().softwareDescription().
manufacturer().
name().
version();
Hardware.Mask hardwareMask = service.withMask().hardware();
hardwareMask.fullyQualifiedDomainName();
hardwareMask.primaryIpAddress();
hardwareMask.primaryBackendIpAddress();
Description.Mask descriptionMask = hardwareMask.operatingSystem().softwareLicense().softwareDescription();
descriptionMask.manufacturer();
descriptionMask.name();
descriptionMask.version();
Guest.Mask guestMask = service.withMask().virtualGuests();
guestMask.fullyQualifiedDomainName();
guestMask.primaryIpAddress();
guestMask.primaryBackendIpAddress();
descriptionMask = guestMask.operatingSystem().softwareLicense().softwareDescription();
descriptionMask.manufacturer();
descriptionMask.name();
descriptionMask.version();

// Calling getObject will now use the mask
// By using asAsync this runs on a separate thread pool, and get() is called on the resulting future
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public void run(ApiClient client) throws Exception {
guest.getId(), minutesToWait);
} else {
// Using a mask, we can ask for the operating system password
service.withNewMask().primaryIpAddress().operatingSystem().passwords();
Guest.Mask mask = service.withNewMask();
mask.primaryIpAddress();
mask.operatingSystem().passwords();
guest = service.getObject();
if (guest.getOperatingSystem() == null) {
System.out.println("Unable to find operating system on completed guest");
Expand Down
49 changes: 40 additions & 9 deletions gen/src/main/java/com/softlayer/api/gen/ClassWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public class ClassWriter extends JavaWriter {
public static final String TYPE_SERVICE = "com.softlayer.api.Service";
public static final String TYPE_SERVICE_ASYNC = "com.softlayer.api.ServiceAsync";
public static final String TYPE_TYPE = "com.softlayer.api.Type";
public static final String TYPE_FILTERABLE_BOOLEAN_PROPERTY = "com.softlayer.api.Property.BooleanProperty";
public static final String TYPE_FILTERABLE_BYTE_ARRAY_PROPERTY = "com.softlayer.api.Property.ByteArrayProperty";
public static final String TYPE_FILTERABLE_DATE_TIME_PROPERTY = "com.softlayer.api.Property.DateTimeProperty";
public static final String TYPE_FILTERABLE_NUMBER_PROPERTY = "com.softlayer.api.Property.NumberProperty";
public static final String TYPE_FILTERABLE_STRING_PROPERTY = "com.softlayer.api.Property.StringProperty";

private static final Set<Modifier> PROTECTED = EnumSet.of(Modifier.PROTECTED);
private static final Set<Modifier> PUBLIC = EnumSet.of(Modifier.PUBLIC);
Expand Down Expand Up @@ -108,8 +113,8 @@ public ClassWriter emitAnnotationWithAttrs(String annotationType, Object... attr
}

public ClassWriter emitMask() throws IOException {
String baseMask = type.baseJavaType != null ? type.baseJavaType + ".Mask" : TYPE_MASK;

String baseMask = type.baseJavaType != null ? type.baseJavaType + ".Mask" : TYPE_MASK;
beginType("Mask", "class", PUBLIC_STATIC, baseMask).emitEmptyLine();

for (TypeClass.Property property : type.properties) {
Expand All @@ -119,13 +124,38 @@ public ClassWriter emitMask() throws IOException {
compressType(property.nonArrayJavaType + ".Mask")).
endMethod().emitEmptyLine();
} else {
beginMethod("Mask", property.name, PUBLIC).
emitStatement("withLocalProperty(%s)", stringLiteral(property.meta.name)).
emitStatement("return this").
endMethod().emitEmptyLine();

String method;
String returnType;

if ("Boolean".equals(property.nonArrayJavaType)) {
method = "withBooleanProperty";
returnType = TYPE_FILTERABLE_BOOLEAN_PROPERTY;
} else if ("byte[]".equals(property.nonArrayJavaType)) {
method = "withByteArrayProperty";
returnType = TYPE_FILTERABLE_BYTE_ARRAY_PROPERTY;
} else if ("java.util.GregorianCalendar".equals(property.nonArrayJavaType)) {
method = "withDateTimeProperty";
returnType = TYPE_FILTERABLE_DATE_TIME_PROPERTY;
} else if ("java.math.BigDecimal".equals(property.nonArrayJavaType)
|| "java.math.BigInteger".equals(property.nonArrayJavaType)
|| "Long".equals(property.nonArrayJavaType)) {
method = "withNumberProperty";
returnType = TYPE_FILTERABLE_NUMBER_PROPERTY;
} else if ("String".equals(property.nonArrayJavaType)) {
method = "withStringProperty";
returnType = TYPE_FILTERABLE_STRING_PROPERTY;
} else {
throw new IllegalArgumentException("Unrecognized primitive type: " + property.nonArrayJavaType);
}
beginMethod(returnType, property.name, PUBLIC)
.emitStatement("return " + method + "(%s)", stringLiteral(property.name))
.endMethod()
.emitEmptyLine();

}
}

endType().emitEmptyLine();
return this;
}
Expand Down Expand Up @@ -213,8 +243,9 @@ public ClassWriter emitService() throws IOException {
beginMethod("ServiceAsync", "asAsync", PUBLIC).endMethod();
beginMethod("Mask", "withNewMask", PUBLIC).endMethod();
beginMethod("Mask", "withMask", PUBLIC).endMethod();
beginMethod("void", "setMask", PUBLIC, "Mask", "mask").endMethod().emitEmptyLine();

beginMethod("Mask", "withNewFilter", PUBLIC).endMethod();
beginMethod("Mask", "withFilter", PUBLIC).endMethod().emitEmptyLine();

for (TypeClass.Method method : type.methods) {
emitServiceMethod(method, false);
}
Expand Down
161 changes: 161 additions & 0 deletions src/main/java/com/softlayer/api/Filter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package com.softlayer.api;

import java.util.Collections;
import java.util.Map;

public abstract class Filter {
protected abstract Map<String, ?> getFilterMap();

public enum SimpleOperation {
EQUAL_TO {
@Override
String withValue(String value) {
requireNotNull(value);
return value;
}
},
NOT_EQUAL_TO {
@Override
String withValue(String value) {
requireNotNull(value);
return "!= " + value;
}
},
GREATER_THAN {
String withValue(String value) {
requireNotNull(value);
return "> " + value;
}
},
GREATER_OR_EQUAL_TO {
@Override
String withValue(String value) {
requireNotNull(value);
return ">= " + value;
}
},
LESS_THAN {
@Override
String withValue(String value) {
requireNotNull(value);
return "< " + value;
}
},
LESS_OR_EQUAL_TO {
@Override
String withValue(String value) {
requireNotNull(value);
return "<= " + value;
}
},
STARTS_WITH {
@Override
String withValue(String value) {
requireNotNull(value);
return "^= " + value;
}
},
NOT_STARTS_WITH {
@Override
String withValue(String value) {
requireNotNull(value);
return "!^= " + value;
}
},
ENDS_WITH {
@Override
String withValue(String value) {
requireNotNull(value);
return "$= " + value;
}
},
NOT_ENDS_WITH {
@Override
String withValue(String value) {
requireNotNull(value);
return "!$= " + value;
}
},
CONTAINS {
@Override
String withValue(String value) {
requireNotNull(value);
return "*= " + value;
}
},
NOT_CONTAINS {
@Override
String withValue(String value) {
requireNotNull(value);
return "!*= " + value;
}
},
EQUAL_TO_IGNORE_CASE {
@Override
String withValue(String value) {
requireNotNull(value);
return "_= " + value;
}
},
NOT_EQUAL_TO_IGNORE_CASE {
@Override
String withValue(String value) {
requireNotNull(value);
return "!_= " + value;
}
},
NOT_NULL {
@Override
String withValue(String value) {
requireNull(value);
return "not null";
}
},
IS_NULL {
@Override
String withValue(String value) {
requireNull(value);
return "null";
}
};

void requireNull(String value) {
if (value != null) {
throw new IllegalArgumentException("Null is required for operation " + this);
}
}

void requireNotNull(String value) {
if (value == null) {
throw new IllegalArgumentException("Null is not allowed for operation " + this);
}
}

abstract String withValue(String value);
}

public static class SimpleFilter<T> extends Filter {
private final SimpleOperation operation;
private final T value;

public SimpleFilter(SimpleOperation operation, T value) {
this.operation = operation;
this.value = value;
}

public SimpleOperation getOperation() {
return operation;
}

public T getValue() {
return value;
}

@Override
protected Map<String, ?> getFilterMap() {
return Collections.singletonMap("operation", getOperation().withValue(
getValue() == null ? null : getValue().toString()
));
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/softlayer/api/Filterable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.softlayer.api;

/**
* Interface implemented by services that accept filters for some calls.
*/
public interface Filterable {
/** Overwrite the existing filter on this object with a new one and return it. */
public Mask withNewFilter();

/** Use the existing filter on this object or create it if not present. */
public Mask withFilter();

/** Set the filter to the given object. */
public void setFilter(Mask filter);

/** Set the filter to a string. */
public void setFilter(String filter);

/** Removes the filter from this object. */
public void clearFilter();
}
Loading