use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.
the class JSONManyAssociationStateTest method givenJSONManyAssociationStateWhenChangingReferencesExpectCorrectBehavior.
@Test
public void givenJSONManyAssociationStateWhenChangingReferencesExpectCorrectBehavior() throws JSONException {
// Fake JSONManyAssociationState
JSONObject state = new JSONObject();
state.put(JSONEntityState.JSON_KEY_PROPERTIES, new JSONObject());
state.put(JSONEntityState.JSON_KEY_ASSOCIATIONS, new JSONObject());
state.put(JSONEntityState.JSON_KEY_MANYASSOCIATIONS, new JSONObject());
JSONEntityState entityState = new JSONEntityState(null, null, "0", System.currentTimeMillis(), EntityReference.parseEntityReference("123"), EntityStatus.NEW, null, state);
JSONManyAssociationState jsonState = new JSONManyAssociationState(entityState, new JSONArray());
assertThat(jsonState.contains(EntityReference.parseEntityReference("NOT_PRESENT")), is(false));
jsonState.add(0, EntityReference.parseEntityReference("0"));
jsonState.add(1, EntityReference.parseEntityReference("1"));
jsonState.add(2, EntityReference.parseEntityReference("2"));
assertThat(jsonState.contains(EntityReference.parseEntityReference("1")), is(true));
assertThat(jsonState.get(0).identity(), equalTo("0"));
assertThat(jsonState.get(1).identity(), equalTo("1"));
assertThat(jsonState.get(2).identity(), equalTo("2"));
assertThat(jsonState.count(), equalTo(3));
jsonState.remove(EntityReference.parseEntityReference("1"));
assertThat(jsonState.count(), equalTo(2));
assertThat(jsonState.contains(EntityReference.parseEntityReference("1")), is(false));
assertThat(jsonState.get(0).identity(), equalTo("0"));
assertThat(jsonState.get(1).identity(), equalTo("2"));
jsonState.add(2, EntityReference.parseEntityReference("1"));
assertThat(jsonState.count(), equalTo(3));
jsonState.add(0, EntityReference.parseEntityReference("A"));
jsonState.add(0, EntityReference.parseEntityReference("B"));
jsonState.add(0, EntityReference.parseEntityReference("C"));
assertThat(jsonState.count(), equalTo(6));
assertThat(jsonState.get(0).identity(), equalTo("C"));
assertThat(jsonState.get(1).identity(), equalTo("B"));
assertThat(jsonState.get(2).identity(), equalTo("A"));
assertThat(jsonState.contains(EntityReference.parseEntityReference("C")), is(true));
assertThat(jsonState.contains(EntityReference.parseEntityReference("B")), is(true));
assertThat(jsonState.contains(EntityReference.parseEntityReference("A")), is(true));
assertThat(jsonState.contains(EntityReference.parseEntityReference("0")), is(true));
assertThat(jsonState.contains(EntityReference.parseEntityReference("2")), is(true));
assertThat(jsonState.contains(EntityReference.parseEntityReference("1")), is(true));
List<String> refList = toList(map(new Function<EntityReference, String>() {
@Override
public String map(EntityReference from) {
return from.identity();
}
}, jsonState));
assertThat(refList.isEmpty(), is(false));
assertArrayEquals(new String[] { "C", "B", "A", "0", "2", "1" }, refList.toArray());
}
use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.
the class ServiceAssemblyImpl method activatorsDeclarations.
protected Iterable<Class<? extends Activator<?>>> activatorsDeclarations(Iterable<? extends Class<?>> typess) {
// Find activator declarations
ArrayList<Type> allTypes = new ArrayList<Type>();
for (Class<?> type : typess) {
Iterable<Type> types = Classes.typesOf(type);
Iterables.addAll(allTypes, types);
}
// Find all activators and flattern them into an iterable
Function<Type, Iterable<Class<? extends Activator<?>>>> function = new Function<Type, Iterable<Class<? extends Activator<?>>>>() {
@Override
public Iterable<Class<? extends Activator<?>>> map(Type type) {
Activators activators = Annotations.annotationOn(type, Activators.class);
if (activators == null) {
return Iterables.empty();
} else {
return Iterables.iterable(activators.value());
}
}
};
Iterable<Class<? extends Activator<?>>> flatten = Iterables.flattenIterables(Iterables.map(function, allTypes));
return Iterables.toList(flatten);
}
use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.
the class DebuggingTest method whenCallingMethodThenExpectDebugEntityCreated.
@Test
public void whenCallingMethodThenExpectDebugEntityCreated() {
UnitOfWork uow = module.newUnitOfWork();
try {
// There is no Query capability available for Libraries, since that sits in Extensions.
// Obtaining the EntityStore directly is a very ugly hack to get around this problem, and only related
// to the test sitting in qi4j-libraries source repository.
// QueryBuilder<DebugRecord> builder = module.newQueryBuilder( DebugRecord.class );
// Query<DebugRecord> query = builder.newQuery( uow );
// assertEquals( 0, query.count() );
Some service = (Some) module.findService(Some.class).get();
String message = service.doSomething("World!", 10);
assertEquals(message, "Hello!");
EntityStore es = (EntityStore) module.findService(EntityStore.class).get();
final String[] result = new String[1];
es.entityStates(module).transferTo(Transforms.map(new Function<EntityState, EntityState>() {
public EntityState map(EntityState entityState) {
if (ServiceDebugRecordEntity.class.getName().equals(first(entityState.entityDescriptor().types()).getName())) {
result[0] = entityState.identity().identity();
}
return entityState;
}
}, Outputs.<EntityState>noop()));
ServiceDebugRecordEntity debugEntry = uow.get(ServiceDebugRecordEntity.class, result[0]);
String mess = debugEntry.message().get();
System.out.println(mess);
assertEquals("some message.", mess);
uow.complete();
} catch (ConcurrentEntityModificationException e) {
e.printStackTrace();
uow.discard();
} catch (UnitOfWorkCompletionException e) {
e.printStackTrace();
uow.discard();
} finally {
if (uow.isOpen()) {
uow.discard();
}
}
}
use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.
the class ConstraintViolationException method localizedMessagesFrom.
/**
* Creates localized messages of all the constraint violations that has occured.
* <p/>
* The key "<code>Qi4j_ConstraintViolation_<i><strong>CompositeType</strong></code></i>" will be used to lookup the text formatting
* pattern from the ResourceBundle, where <strong><code><i>CompositeType</i></code></strong> is the
* class name of the Composite where the constraint was violated. If such key does not exist, then the
* key "<code>Qi4j_ConstraintViolation</code>" will be used, and if that one also doesn't exist, or
* the resourceBundle argument is null, then the default patterns will be used;
* <table><tr><th>Type of Composite</th><th>Pattern used</th></tr>
* <tr><td>Composite</td>
* <td><code>Constraint Violation in {2}.{3} with constraint {4}, in composite \n{0} of type {1}</code></td>
* </tr>
* <tr><td>EntityComposite</td>
* <td><code>Constraint Violation in {2}.{3} with constraint {4}, in entity {1}[id={0}]</code></td>
* </tr>
* <tr><td>ServiceComposite</td>
* <td><code>Constraint Violation in {2}.{3} with constraint {4}, in service {0}</code></td>
* </tr>
* </table>
* Then format each ConstraintViolation according to such pattern, where the following argument are passed;
* <table><tr><th>Arg</th><th>Value</th></tr>
* <tr>
* <td>{0}</td>
* <td>Composite instance toString()</td>
* </tr>
* <tr>
* <td>{1}</td>
* <td>CompositeType class name</td>
* </tr>
* <tr>
* <td>{2}</td>
* <td>MixinType class name</td>
* </tr>
* <tr>
* <td>{3}</td>
* <td>MixinType method name</td>
* </tr>
* <tr>
* <td>{4}</td>
* <td>Annotation toString()</td>
* </tr>
* <tr>
* <td>{5}</td>
* <td>toString() of value passed as the argument, or "null" text if argument was null.</td>
* </tr>
* </table>
* <p/>
* <b>NOTE!!!</b> This class is still under construction and will be modified further.
*
* @param bundle The ResourceBundle for Localization, or null if default formatting and locale to be used.
*
* @return An array of localized messages of the violations incurred.
*/
public String[] localizedMessagesFrom(ResourceBundle bundle) {
String pattern = "Constraint violation in {0}.{1} for method ''{3}'' with constraint \"{4}({6})\", for value ''{5}''";
ArrayList<String> list = new ArrayList<String>();
for (ConstraintViolation violation : constraintViolations) {
Locale locale;
if (bundle != null) {
try {
pattern = bundle.getString("qi4j.constraint." + mixinTypeName + "." + methodName);
} catch (MissingResourceException e1) {
try {
pattern = bundle.getString("qi4j.constraint");
} catch (MissingResourceException e2) {
// ignore. The default pattern will be used.
}
}
locale = bundle.getLocale();
} else {
locale = Locale.getDefault();
}
MessageFormat format = new MessageFormat(pattern, locale);
Annotation annotation = violation.constraint();
String name = violation.name();
Object value = violation.value();
String classes;
if (Iterables.count(instanceTypes) == 1) {
classes = Iterables.first(instanceTypes).getSimpleName();
} else {
classes = "[" + Iterables.<Class<?>>toString(instanceTypes, new Function<Class<?>, String>() {
@Override
public String map(Class<?> from) {
return from.getSimpleName();
}
}, ",") + "]";
}
Object[] args = new Object[] { instanceToString, classes, mixinTypeName, methodName, annotation.toString(), "" + value, name };
StringBuffer text = new StringBuffer();
format.format(args, text, null);
list.add(text.toString());
}
String[] result = new String[list.size()];
list.toArray(result);
return result;
}
use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.
the class TableBuilder method rows.
public TableBuilder rows(Iterable<?> rowObjects) {
boolean no_format = false;
boolean no_values = false;
if (tableQuery != null && tableQuery.options() != null) {
if (tableQuery.options().contains("no_format"))
no_format = true;
if (tableQuery != null && tableQuery.options().contains("no_values"))
no_values = true;
}
for (Object rowObject : rowObjects) {
row();
for (Column column : tableBuilder.prototype().cols().get()) {
Object v = null;
String f = null;
Function valueFunction = columns.get(column.id().get()).getValueFunction();
if (!no_values && valueFunction != null)
v = valueFunction.map(rowObject);
Function formattedFunction = columns.get(column.id().get()).getFormattedFunction();
if (!no_format && formattedFunction != null)
f = (String) formattedFunction.map(rowObject);
else if (v != null) {
if (column.columnType().get().equals(Table.DATETIME))
f = Dates.toUtcString((Date) v);
else if (column.columnType().get().equals(Table.DATE))
f = new SimpleDateFormat("yyyy-MM-dd").format((Date) v);
else if (column.columnType().get().equals(Table.TIME_OF_DAY))
f = new SimpleDateFormat("HH:mm:ss").format((Date) v);
else
f = v.toString();
}
cell(v, f);
}
endRow();
}
return this;
}
Aggregations