org.deduced.rule
Class DeductionRuleLibrary

java.lang.Object
  extended by org.deduced.rule.DeductionRuleLibrary
All Implemented Interfaces:
DeductionRuleAnalyzer<java.lang.Object>
Direct Known Subclasses:
IsCreateTypeValidRule, IsNullableValidRule, IsNumberValidRule, IsReferenceTypeValidRule, IsStringValidRule, SetIsValidBasedOnErrorMessageRule

public abstract class DeductionRuleLibrary
extends java.lang.Object
implements DeductionRuleAnalyzer<java.lang.Object>

DeductionRuleLibrary offers a set of quick methods to implement deduction rules

Author:
Steve McDuff

Nested Class Summary
static class DeductionRuleLibrary.CompareResult
          CompareResult is an utility class to store a collection and a value
 
Field Summary
private static java.util.logging.Logger LOGGER
          Logger
 
Constructor Summary
DeductionRuleLibrary()
           
 
Method Summary
static PropertyCollection addReferenceToList(PropertyCollection referenceToAdd, PropertyCollection list, boolean allowDuplicateReference)
          add a reference to a list.
private static void addToRemoveListIfNotMatched(boolean removeUnmatchedValues, java.util.List<PropertyCollection> objectsToRemove, java.util.Collection valuesToMatchClone, PropertyCollection currentCollection)
          add a collection To the Remove List If Not Matched
static Filter and(Filter firstFilter, Filter secondFilter)
          create an "and" filter
static Filter bigger(java.lang.Object value)
          Create a bigger than filter.
static Filter biggerOrEqual(java.lang.Object value)
          Create a bigger or equal to filter.
static java.util.List<java.util.Calendar> createDateIncrementList(java.util.Calendar startDate, java.util.Calendar endDate, long incrementInMilliseconds)
          create a Date Increment List
static PropertyCollection createObject(PropertyCollection typeToCreate, RuleExecutionContext context)
          create an Object
static PropertyCollection createObjectsMatchingAllValues(java.util.List valuesToMatch, PropertyCollection listToAddMissingValues, PropertyCollection instanceToMatch, PropertyCollection typeToCreate, boolean deleteUnmatchedValues, RuleExecutionContext context)
          create Objects Matching All Values and add them to the specified list
static PropertyCollection createObjectsMatchingAllValues(java.util.List valuesToMatch, PropertyCollection listToAddMissingValues, PropertyCollection instanceToMatch, PropertyCollection typeToCreate, RuleExecutionContext context)
          create Objects Matching All Values and add them to the specified list
private static void createUnmatchedValues(PropertyCollection listToAddMissingValues, PropertyCollection<?,?> collectionTypeToCreate, java.lang.Object instanceKey, java.util.List valuesToMatchClone, ModelFactory modelFactory)
          createUnmatchedValues
static void debug(java.lang.String message)
          utility method to output messages in the console quickly
static Filter equalTo(java.lang.Object equalValue)
          create a filter to where objects have to equal a specific value
static PropertyCollection fillList(PropertyCollection listToFill, java.util.List objectsToAddToList, boolean removeUnmatchedValues)
          fill a List with a list of objects.
private static void fillListCreateUnmatchedValues(PropertyCollection listToFill, java.util.Collection valuesToMatchClone)
          fill List Create Unmatched Values
private static void fillListExtractMatchesValues(boolean removeUnmatchedValues, java.util.List currentValueCollectionList, java.util.List<PropertyCollection> objectsToRemove, java.util.Collection valuesToMatchClone)
          fill List Extract Matches Values
private static void fillListRemoveUnmatchedValues(PropertyCollection listToFill, boolean isReference, java.util.List<PropertyCollection> objectsToRemove)
          fill List Remove Unmatched Values
static
<T> java.util.List<T>
filter(java.util.List<T> listToFilter, Filter condition)
          filter a list based on a filter
static java.lang.Object filterList(java.util.Collection listToFilter, boolean nullOnMultipleResult)
          filter a collection to extract a single object from it.
static PropertyCollection<?,?> filterList(PropertyCollection<?,?> listToFilter, boolean nullOnMultipleResult)
          filter property collection to extract a single object from it.
static
<T> T
filterList(T[] listToFilter, boolean nullOnMultipleResult)
          filter an array property collection to extract a single object from it.
private static DeductionRuleLibrary.CompareResult findMaximumResult(java.util.List collectionList, PropertyCollection instanceToCompare)
          find the maximum result in the list of property collection for the specified instance
private static DeductionRuleLibrary.CompareResult findMinimumResult(java.util.List<?> collectionList, PropertyCollection<?,?> instanceToCompare)
          find the minimum result in the list of property collection for the specified instance
static PropertyCollection findParentOfType(PropertyCollection searchPoint, PropertyCollection type, boolean matchExactType)
          find Parent Of Type
static java.lang.String formatDate(java.util.Calendar value)
          format a date to show the year, month and day
static java.lang.String formatDate(java.util.Calendar value, java.lang.String dateFormat)
          format Date to a string with a specific display format
static java.lang.String formatTime(java.util.Calendar value)
          format Time
static Filter instanceOf(java.lang.Object type)
          create a filter to where objects have to be of a specific type
private static boolean isContextValid(RuleExecutionContext context)
          is Context Valid
private static boolean isCreationOptionsValid(java.util.List valuesToMatch, PropertyCollection listToAddMissingValues, PropertyCollection instanceToMatch, PropertyCollection typeToCreate, RuleExecutionContext context)
          test if the options used to create collections are all valid
private static boolean isTypeToCreateValid(PropertyCollection<?,?> typeToCreate)
          is Type To Create Valid
static PropertyCollection maximumCollection(java.util.List collectionList, PropertyCollection instanceToCompare)
          find the property collection in the list that has the maximum value for the specified instance
static java.lang.Object maximumValue(java.util.List collectionList, PropertyCollection instanceToCompare)
          find the maximum value in the list of property collection for the specified instance
static java.util.List merge(java.util.List firstList, java.util.List secondList)
          merge two list of objects together and return a list that contains the objects of the first list followed by the second list.
static PropertyCollection minimumCollection(java.util.List collectionList, PropertyCollection instanceToCompare)
          find the property collection in the list that has the minimum value for the specified instance
static java.lang.Object minimumValue(java.util.List collectionList, PropertyCollection instanceToCompare)
          find the minimum value in the list of property collection for the specified instance
static Filter not(Filter otherFilter)
          Create a reverse filter
static Filter or(Filter firstFilter, Filter secondFilter)
          create an "or" filter
static Filter property(PropertyCollection<?,?> instance, Filter propertyValueFilter)
          create a filter based on the value of a property and a filter applied to that value
private static void removeAlreadyMatchedValues(boolean deleteUnmatchedValues, java.lang.Object instanceKey, java.util.List valuesToMatchClone, java.util.List<PropertyCollection> valuesToRemove, java.lang.Object possibleCollection)
          remove Already Matched Values
static Filter same(java.lang.Object sameInstance)
          create a filter to where objects have to be the same as a specific value
static Filter smaller(java.lang.Object value)
          Create a smaller than filter.
static Filter smallerOrEqual(java.lang.Object value)
          Create a smaller or equal to filter.
static int sum(java.lang.Integer first, java.lang.Integer second)
          sum two integers
static double sum(java.util.List listOfNumbers)
          sum a list of numbers
private static void updateMaximumResultBasedOnCollection(DeductionRuleLibrary.CompareResult retVal, java.lang.Integer instanceIdentityKey, PropertyCollection collection)
          update Maximum Result Based On Collection
private static void updateMinimumResultWithCollection(DeductionRuleLibrary.CompareResult retVal, java.lang.Integer instanceIdentityKey, PropertyCollection<?,?> collection)
          update Minimum Result With Collection
private static void validateAddTypeToList(PropertyCollection list, PropertyCollection<?,?> typeToAdd)
          validate if an object of the specified property collection type can be added to a property collection
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.deduced.DeductionRuleAnalyzer
analyzeRule
 

Field Detail

LOGGER

private static final java.util.logging.Logger LOGGER
Logger

Constructor Detail

DeductionRuleLibrary

public DeductionRuleLibrary()
Method Detail

filterList

public static java.lang.Object filterList(java.util.Collection listToFilter,
                                          boolean nullOnMultipleResult)
filter a collection to extract a single object from it. This method will return the first object in the list.

Parameters:
listToFilter - the list to filter
nullOnMultipleResult - if this flag is set to true and the list contains more than 1 object, null will be returned.
Returns:
a single object from the list

filterList

public static PropertyCollection<?,?> filterList(PropertyCollection<?,?> listToFilter,
                                                 boolean nullOnMultipleResult)
filter property collection to extract a single object from it. This method will return the first object in the list.

Parameters:
listToFilter - the list to filter
nullOnMultipleResult - if this flag is set to true and the list contains more than 1 object, null will be returned.
Returns:
a single object from the list

filterList

public static <T> T filterList(T[] listToFilter,
                               boolean nullOnMultipleResult)
filter an array property collection to extract a single object from it. This method will return the first object in the list.

Type Parameters:
T - the type of object in the array
Parameters:
listToFilter - the list to filter
nullOnMultipleResult - if this flag is set to true and the list contains more than 1 object, null will be returned.
Returns:
a single object from the list

sum

public static int sum(java.lang.Integer first,
                      java.lang.Integer second)
sum two integers

Parameters:
first - the first integer
second - the second integer
Returns:
the sum of both integers. This method will overflow if the sum of both values is larger or smaller than what can be contained in an integer.

sum

public static double sum(java.util.List listOfNumbers)
sum a list of numbers

Parameters:
listOfNumbers - the list of numbers
Returns:
the sum of all the numbers

minimumCollection

public static PropertyCollection minimumCollection(java.util.List collectionList,
                                                   PropertyCollection instanceToCompare)
find the property collection in the list that has the minimum value for the specified instance

Parameters:
collectionList - the collection list to search in
instanceToCompare - the instance to look for in the comparison of values
Returns:
the collection that has the smallest value

minimumValue

public static java.lang.Object minimumValue(java.util.List collectionList,
                                            PropertyCollection instanceToCompare)
find the minimum value in the list of property collection for the specified instance

Parameters:
collectionList - the collection list to search in
instanceToCompare - the instance to look for in the comparison of values
Returns:
the smallest value

findMinimumResult

private static DeductionRuleLibrary.CompareResult findMinimumResult(java.util.List<?> collectionList,
                                                                    PropertyCollection<?,?> instanceToCompare)
find the minimum result in the list of property collection for the specified instance

Parameters:
collectionList - the collection list to search in
instanceToCompare - the instance to look for in the comparison of values
Returns:
the smallest result

updateMinimumResultWithCollection

private static void updateMinimumResultWithCollection(DeductionRuleLibrary.CompareResult retVal,
                                                      java.lang.Integer instanceIdentityKey,
                                                      PropertyCollection<?,?> collection)
update Minimum Result With Collection

Parameters:
retVal - the compare result to update
instanceIdentityKey - the instance key to read
collection - the collection to read

maximumCollection

public static PropertyCollection maximumCollection(java.util.List collectionList,
                                                   PropertyCollection instanceToCompare)
find the property collection in the list that has the maximum value for the specified instance

Parameters:
collectionList - the collection list to search in
instanceToCompare - the instance to look for in the comparison of values
Returns:
the collection that has the maximum value

maximumValue

public static java.lang.Object maximumValue(java.util.List collectionList,
                                            PropertyCollection instanceToCompare)
find the maximum value in the list of property collection for the specified instance

Parameters:
collectionList - the collection list to search in
instanceToCompare - the instance to look for in the comparison of values
Returns:
the maximum value

findMaximumResult

private static DeductionRuleLibrary.CompareResult findMaximumResult(java.util.List collectionList,
                                                                    PropertyCollection instanceToCompare)
find the maximum result in the list of property collection for the specified instance

Parameters:
collectionList - the collection list to search in
instanceToCompare - the instance to look for in the comparison of values
Returns:
the maximum result

updateMaximumResultBasedOnCollection

private static void updateMaximumResultBasedOnCollection(DeductionRuleLibrary.CompareResult retVal,
                                                         java.lang.Integer instanceIdentityKey,
                                                         PropertyCollection collection)
update Maximum Result Based On Collection

Parameters:
retVal - the result to update
instanceIdentityKey - the instance key to read
collection - the collection to read

formatDate

public static java.lang.String formatDate(java.util.Calendar value)
format a date to show the year, month and day

Parameters:
value - the value to format
Returns:
the formatted string

formatTime

public static java.lang.String formatTime(java.util.Calendar value)
format Time

Parameters:
value - the time to format in a string
Returns:
the formatted string

formatDate

public static java.lang.String formatDate(java.util.Calendar value,
                                          java.lang.String dateFormat)
format Date to a string with a specific display format

Parameters:
value - the value to format
dateFormat - the date format
Returns:
the formatted date

fillList

public static PropertyCollection fillList(PropertyCollection listToFill,
                                          java.util.List objectsToAddToList,
                                          boolean removeUnmatchedValues)
fill a List with a list of objects.

Parameters:
listToFill - the list to fill
objectsToAddToList - objects to add to the list
removeUnmatchedValues - if this option is set to true, all the objects in the original list that don't match the list of objects to add will be removed.
Returns:
the filled list. Same as the listToFill parameter.

fillListExtractMatchesValues

private static void fillListExtractMatchesValues(boolean removeUnmatchedValues,
                                                 java.util.List currentValueCollectionList,
                                                 java.util.List<PropertyCollection> objectsToRemove,
                                                 java.util.Collection valuesToMatchClone)
fill List Extract Matches Values

Parameters:
removeUnmatchedValues - the option to remove unmatched values
currentValueCollectionList - the list of all the values that need to be matched
objectsToRemove - the list of objects to potentially remove
valuesToMatchClone - the values to match

fillListCreateUnmatchedValues

private static void fillListCreateUnmatchedValues(PropertyCollection listToFill,
                                                  java.util.Collection valuesToMatchClone)
fill List Create Unmatched Values

Parameters:
listToFill - the list to fill
valuesToMatchClone - the values to add

fillListRemoveUnmatchedValues

private static void fillListRemoveUnmatchedValues(PropertyCollection listToFill,
                                                  boolean isReference,
                                                  java.util.List<PropertyCollection> objectsToRemove)
fill List Remove Unmatched Values

Parameters:
listToFill - the list to fill
isReference - is the value to remove by reference
objectsToRemove - the list of values to remove

addToRemoveListIfNotMatched

private static void addToRemoveListIfNotMatched(boolean removeUnmatchedValues,
                                                java.util.List<PropertyCollection> objectsToRemove,
                                                java.util.Collection valuesToMatchClone,
                                                PropertyCollection currentCollection)
add a collection To the Remove List If Not Matched

Parameters:
removeUnmatchedValues - option to remove unmatched values
objectsToRemove - the object to remove list
valuesToMatchClone - the values to match
currentCollection - the current collection

createObject

public static PropertyCollection createObject(PropertyCollection typeToCreate,
                                              RuleExecutionContext context)
create an Object

Parameters:
typeToCreate - the type of object to create
context - the rule execution context
Returns:
the created object

createObjectsMatchingAllValues

public static PropertyCollection createObjectsMatchingAllValues(java.util.List valuesToMatch,
                                                                PropertyCollection listToAddMissingValues,
                                                                PropertyCollection instanceToMatch,
                                                                PropertyCollection typeToCreate,
                                                                RuleExecutionContext context)
create Objects Matching All Values and add them to the specified list

Parameters:
valuesToMatch - the values to match
listToAddMissingValues - the list to fill
instanceToMatch - the instance where to set the value on each created object
typeToCreate - the type of object to create
context - the rule execution context
Returns:
the list that was filled. Same as listToAddMissingValues

createObjectsMatchingAllValues

public static PropertyCollection createObjectsMatchingAllValues(java.util.List valuesToMatch,
                                                                PropertyCollection listToAddMissingValues,
                                                                PropertyCollection instanceToMatch,
                                                                PropertyCollection typeToCreate,
                                                                boolean deleteUnmatchedValues,
                                                                RuleExecutionContext context)
create Objects Matching All Values and add them to the specified list

Parameters:
valuesToMatch - the values to match
listToAddMissingValues - the list to fill
instanceToMatch - the instance where to set the value on each created object
typeToCreate - the type of object to create
deleteUnmatchedValues - if this option is set to true, all the objects in the original list that don't match the list of values to add will be removed.
context - the rule execution context
Returns:
the list that was filled. Same as listToAddMissingValues

isCreationOptionsValid

private static boolean isCreationOptionsValid(java.util.List valuesToMatch,
                                              PropertyCollection listToAddMissingValues,
                                              PropertyCollection instanceToMatch,
                                              PropertyCollection typeToCreate,
                                              RuleExecutionContext context)
test if the options used to create collections are all valid

Parameters:
valuesToMatch - the list of values to match
listToAddMissingValues - the list on which to add the missing values
instanceToMatch - the instance to match
typeToCreate - the type to create
context - the context. The context must have a valid model factory
Returns:
true if all the parameters are correct to proceed with object creation

isTypeToCreateValid

private static boolean isTypeToCreateValid(PropertyCollection<?,?> typeToCreate)
is Type To Create Valid

Parameters:
typeToCreate - the type to create
Returns:
true if the type is valid

isContextValid

private static boolean isContextValid(RuleExecutionContext context)
is Context Valid

Parameters:
context - the context
Returns:
true if the context is valid

createUnmatchedValues

private static void createUnmatchedValues(PropertyCollection listToAddMissingValues,
                                          PropertyCollection<?,?> collectionTypeToCreate,
                                          java.lang.Object instanceKey,
                                          java.util.List valuesToMatchClone,
                                          ModelFactory modelFactory)
createUnmatchedValues

Parameters:
listToAddMissingValues - the list on which to add the missing values
collectionTypeToCreate - the type of collection to create
instanceKey - the instance key to set
valuesToMatchClone - the list of values for which we should create collections
modelFactory - the model factory used for creation

removeAlreadyMatchedValues

private static void removeAlreadyMatchedValues(boolean deleteUnmatchedValues,
                                               java.lang.Object instanceKey,
                                               java.util.List valuesToMatchClone,
                                               java.util.List<PropertyCollection> valuesToRemove,
                                               java.lang.Object possibleCollection)
remove Already Matched Values

Parameters:
deleteUnmatchedValues - option to delete unmatched values
instanceKey - the instance key to read
valuesToMatchClone - the values to match list
valuesToRemove - the values to remove list
possibleCollection - the possible collection to remove

createDateIncrementList

public static java.util.List<java.util.Calendar> createDateIncrementList(java.util.Calendar startDate,
                                                                         java.util.Calendar endDate,
                                                                         long incrementInMilliseconds)
create a Date Increment List

Parameters:
startDate - the start date inclusively
endDate - the end date inclusively
incrementInMilliseconds - the increment between each step in milliseconds
Returns:
the created list of calendar

filter

public static <T> java.util.List<T> filter(java.util.List<T> listToFilter,
                                           Filter condition)
filter a list based on a filter

Type Parameters:
T - Type of objects in the list to filter
Parameters:
listToFilter - the list to filter
condition - the condition filter
Returns:
the filtered list. This list will always be different from the listToFilter parameter. It can be manipulated on it's own.

bigger

public static Filter bigger(java.lang.Object value)
Create a bigger than filter.

Parameters:
value - the bigger than value on the filter.
Returns:
the created filter

smaller

public static Filter smaller(java.lang.Object value)
Create a smaller than filter.

Parameters:
value - the smaller than value on the filter.
Returns:
the created filter

biggerOrEqual

public static Filter biggerOrEqual(java.lang.Object value)
Create a bigger or equal to filter.

Parameters:
value - the bigger or equal to value on the filter.
Returns:
the created filter

smallerOrEqual

public static Filter smallerOrEqual(java.lang.Object value)
Create a smaller or equal to filter.

Parameters:
value - the smaller or equal to value on the filter.
Returns:
the created filter

not

public static Filter not(Filter otherFilter)
Create a reverse filter

Parameters:
otherFilter - the filter to reverse
Returns:
the created filter

equalTo

public static Filter equalTo(java.lang.Object equalValue)
create a filter to where objects have to equal a specific value

Parameters:
equalValue - the equal value
Returns:
the created filter

property

public static Filter property(PropertyCollection<?,?> instance,
                              Filter propertyValueFilter)
create a filter based on the value of a property and a filter applied to that value

Parameters:
instance - the property instance to filter
propertyValueFilter - the filter to apply to the value
Returns:
the created filter

same

public static Filter same(java.lang.Object sameInstance)
create a filter to where objects have to be the same as a specific value

Parameters:
sameInstance - the same value
Returns:
the created filter

instanceOf

public static Filter instanceOf(java.lang.Object type)
create a filter to where objects have to be of a specific type

Parameters:
type - the type to match
Returns:
the created filter

and

public static Filter and(Filter firstFilter,
                         Filter secondFilter)
create an "and" filter

Parameters:
firstFilter - first filter
secondFilter - second filter
Returns:
the created filter

or

public static Filter or(Filter firstFilter,
                        Filter secondFilter)
create an "or" filter

Parameters:
firstFilter - first filter
secondFilter - second filter
Returns:
the created filter

findParentOfType

public static PropertyCollection findParentOfType(PropertyCollection searchPoint,
                                                  PropertyCollection type,
                                                  boolean matchExactType)
find Parent Of Type

Parameters:
searchPoint - the collection to search for a parent of the specified type
type - the type of the parent we are looking for
matchExactType - if this option is true, the parents must match the exact specified type to be returned. Otherwise any type that derives from the specified type will be returned.
Returns:
the parent matching the type. Null if no matching parents could be found.

merge

public static java.util.List merge(java.util.List firstList,
                                   java.util.List secondList)
merge two list of objects together and return a list that contains the objects of the first list followed by the second list.

Parameters:
firstList - the first list to add. This parameter can be null.
secondList - the second list to add. This parameter can be null.
Returns:
the merged list. This value is never null, even if both inputs are.

debug

public static void debug(java.lang.String message)
utility method to output messages in the console quickly

Parameters:
message - the message to show

addReferenceToList

public static PropertyCollection addReferenceToList(PropertyCollection referenceToAdd,
                                                    PropertyCollection list,
                                                    boolean allowDuplicateReference)
add a reference to a list. This method will ensure that it is valid to add such a reference. It will also check for duplicate references if they aren't allowed.

Parameters:
referenceToAdd - the reference to add. If this parameter is null, nothing occurs.
list - the list to which we are adding the reference. If this parameter is null, nothing occurs.
allowDuplicateReference - flag indicating if the list is allowed to contain duplicate references of the same object.
Returns:
the list after it was modified
Throws:
java.lang.IllegalArgumentException - if the list doesn't allow addition of references of the specified type or if the list doesn't contain references.

validateAddTypeToList

private static void validateAddTypeToList(PropertyCollection list,
                                          PropertyCollection<?,?> typeToAdd)
validate if an object of the specified property collection type can be added to a property collection

Parameters:
list - the property collection list
typeToAdd - the type to add
Throws:
java.lang.IllegalArgumentException - if the type to add can't be added.