Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-2015 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  * either express or implied. See the License for the specific language
  * governing permissions and limitations under the License.
  */
 package org.dbflute.dbmeta.accessory;
 
 import java.util.Map;
 
The derived map of entity. (basically for Framework)

Author(s):
jflute
Since:
1.1.0 (2014/10/29 Monday)
 
 public class EntityDerivedMap implements Serializable {
 
     // ===================================================================================
     //                                                                          Definition
     //                                                                          ==========
     
The serial version UID for object serialization. (Default)
 
     private static final long serialVersionUID = 1L;
 
     // ===================================================================================
     //                                                                           Attribute
     //                                                                           =========
     
The map of derived value. map:{alias-name = value} (NullAllowed: lazy-loaded)
 
     protected Map<StringObject_derivedMap;
 
     // ===================================================================================
     //                                                                            Register
     //                                                                            ========
     
Register the derived value to the map.

Parameters:
aliasName The alias name of derived-referrer. (NotNull)
selectedValue The derived value selected from database. (NullAllowed: when null selected)
 
     public void registerDerivedValue(String aliasNameObject selectedValue) {
         getDerivedMap().put(aliasNameselectedValue);
     }
 
     // ===================================================================================
     //                                                                               Find
     //                                                                              ======
     
Find the derived value in the map.

Parameters:
<VALUE> The type of derived value.
entity The entity that has the derived value, basically for logging. (NotNull)
aliasName The alias name of derived-referrer. (NotNull)
propertyType The type of the derived property, should match as rule. (NotNull)
Returns:
The optional scalar for derived value found in the map. (NotNull, EmptyAllowed: when null selected)
Throws:
org.dbflute.exception.SpecifyDerivedReferrerInvalidAliasNameException When the alias name does not start with '$'.
org.dbflute.exception.SpecifyDerivedReferrerUnknownAliasNameException When the alias name is unknown, no derived.
org.dbflute.exception.SpecifyDerivedReferrerUnmatchedPropertyTypeException When the property type is unmatched with actual type.
 
     public <VALUE> OptionalScalar<VALUE> findDerivedValue(Entity entityString aliasNameClass<VALUE> propertyType) {
         if (aliasName == null) {
             throw new IllegalArgumentException("The argument 'aliasName' should not be null.");
         }
         if (propertyType == null) {
             throw new IllegalArgumentException("The argument 'propertyType' should not be null.");
         }
         if (aliasName.trim().length() == 0) {
             throw new IllegalArgumentException("The argument 'aliasName' should not be empty: [" + aliasName + "]");
         }
         if (!aliasName.startsWith(.)) {
             throwInvalidDerivedAliasNameException(entityaliasName);
         }
         final Map<StringObjectderivedMap = getDerivedMap(); // lazy-loaded if nothing
         if (!derivedMap.containsKey(aliasName)) {
             throwUnknownDerivedAliasNameException(entityaliasNamederivedMap);
         }
         try {
             final VALUE found = propertyType.cast(derivedMap.get(aliasName)); // also checking type
             final String tableDbName = entity.asTableDbName(); // not to have reference to entity in optional
             return OptionalScalar.ofNullable(found, () -> {
                 throwDerivedPropertyValueNotFoundException(tableDbNamealiasName);
             }); // null allowed
         } catch (ClassCastException e) {
            throwUnmatchDerivedPropertyTypeException(entityaliasNamepropertyTypee);
            return null// unreachable
        }
    }
    // ===================================================================================
    //                                                                              Others
    //                                                                              ======
    
Is the derived map empty?

Returns:
The determination, true or false.
    public boolean isEmpty() {
        return getDerivedMap().isEmpty();
    }

    
Clear the derived map.
    public void clear() {
        getDerivedMap().clear();
    }

    
Remove the derived value from the map.

Parameters:
aliasName The alias name of derived-referrer. (NotNull)
    public void remove(String aliasName) {
        getDerivedMap().remove(aliasName);
    }
    // ===================================================================================
    //                                                                         Derived Map
    //                                                                         ===========
    protected Map<StringObjectgetDerivedMap() {
        if ( == null) {
             = new HashMap<StringObject>(4);
        }
        return ;
    }
    // ===================================================================================
    //                                                                    Exception Helper
    //                                                                    ================
    protected void throwInvalidDerivedAliasNameException(Entity entityString aliasName) {
        final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
        br.addNotice("Illegal alias name (not start with '$') for the derived property.");
        br.addItem("Advice");
        br.addElement("Make sure your alias name to find the derived value.");
        br.addElement("You should specify the name that starts with '$'.");
        br.addElement("For example:");
        br.addElement("  (x):");
        br.addElement("    cb.specify().derivedPurchaseList().max(purchaseCB -> {");
        br.addElement("        purchaseCB.specify().columnPurchasePrice();");
        br.addElement("    }, \"$HIGHEST_PURCHASE_PRICE\");");
        br.addElement("    ...");
        br.addElement("    Member member = ...");
        br.addElement("    ... = member.derived(\"HIGHEST_PURCHASE_PRICE\", Integer.class); // *NG");
        br.addElement("  (o):");
        br.addElement("    cb.specify().derivedPurchaseList().max(purchaseCB -> {");
        br.addElement("        purchaseCB.specify().columnPurchasePrice();");
        br.addElement("    }, \"$HIGHEST_PURCHASE_PRICE\");");
        br.addElement("    ...");
        br.addElement("    Member member = ...");
        br.addElement("    ... = member.derived(\"$HIGHEST_PURCHASE_PRICE\", Integer.class); // OK");
        br.addItem("Table");
        buildExceptionTableInfo(brentity);
        br.addItem("Illegal Alias");
        br.addElement(aliasName);
        final String msg = br.buildExceptionMessage();
        throw new SpecifyDerivedReferrerInvalidAliasNameException(msg);
    }
    protected void throwUnknownDerivedAliasNameException(Entity entityString aliasNameMap<StringObjectderivedMap) {
        final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
        br.addNotice("Not found the alias name in the derived map");
        br.addItem("Advice");
        br.addElement("Make sure your alias name to find the derived value.");
        br.addElement("You should specify the name specified as DerivedReferrer.");
        br.addElement("For example:");
        br.addElement("  (x):");
        br.addElement("    String highestAlias = \"$HIGHEST_PURCHASE_PRICE\"");
        br.addElement("    cb.specify().derivedPurchaseList().max(purchaseCB -> {");
        br.addElement("        purchaseCB.specify().columnPurchasePrice();");
        br.addElement("    }, highestAlias);");
        br.addElement("    ...");
        br.addElement("    Member member = ...");
        br.addElement("    ... = member.derived(\"$BIG_SHOT\", Integer.class); // *NG");
        br.addElement("  (o):");
        br.addElement("    String highestAlias = \"$HIGHEST_PURCHASE_PRICE\"");
        br.addElement("    cb.specify().derivedPurchaseList().max(purchaseCB -> {");
        br.addElement("        purchaseCB.specify().columnPurchasePrice();");
        br.addElement("    }, highestAlias);");
        br.addElement("    ...");
        br.addElement("    Member member = ...");
        br.addElement("    ... = member.derived(highestAlias, Integer.class); // OK");
        br.addItem("Table");
        buildExceptionTableInfo(brentity);
        br.addItem("Unknown Alias");
        br.addElement(aliasName);
        buildExceptionExistingDerivedMapInfo(brderivedMap);
        final String msg = br.buildExceptionMessage();
        throw new SpecifyDerivedReferrerUnknownAliasNameException(msg);
    }
    protected void throwUnmatchDerivedPropertyTypeException(Entity entityString aliasNameClass<?> propertyTypeClassCastException cause) {
        final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
        br.addNotice("Illegal property type for the derived property.");
        br.addItem("Advice");
        br.addElement("Make sure your property type to find the derived value.");
        br.addElement("You should specify the matched type, it's rule is following:");
        br.addElement("  count()      : Integer");
        br.addElement("  max(), min() : (same as property type of the column)");
        br.addElement("  sum(), avg() : BigDecimal");
        br.addElement("");
        br.addElement("For example:");
        br.addElement("  (x):");
        br.addElement("    String highestAlias = \"$HIGHEST_PURCHASE_PRICE\"");
        br.addElement("    cb.specify().derivedPurchaseList().max(purchaseCB -> {");
        br.addElement("        purchaseCB.specify().columnPurchasePrice();");
        br.addElement("    }, highestAlias);");
        br.addElement("    ...");
        br.addElement("    Member member = ...");
        br.addElement("    ... = member.derived(highestAlias, LocalDate.class); // *NG");
        br.addElement("  (o):");
        br.addElement("    String highestAlias = \"$HIGHEST_PURCHASE_PRICE\"");
        br.addElement("    cb.specify().derivedPurchaseList().max(purchaseCB -> {");
        br.addElement("        purchaseCB.specify().columnPurchasePrice();");
        br.addElement("    }, highestAlias);");
        br.addElement("    ...");
        br.addElement("    Member member = ...");
        br.addElement("    ... = member.derived(highestAlias, Integer.class); // OK");
        buildExceptionTableInfo(brentity);
        br.addItem("NotFound Alias");
        br.addElement(aliasName);
        br.addItem("Specified PropertyType");
        br.addElement(propertyType);
        final String msg = br.buildExceptionMessage();
        throw new SpecifyDerivedReferrerUnmatchedPropertyTypeException(msgcause);
    }
    protected void buildExceptionTableInfo(ExceptionMessageBuilder brEntity entity) {
        br.addItem("Table");
        br.addElement(entity.asTableDbName());
        try {
            br.addElement(entity.asDBMeta().extractPrimaryKeyMap(entity));
        } catch (RuntimeException continued) { // just in case
            br.addElement("*Failed to get PK info:");
            br.addElement(continued.getMessage());
        }
    }
    protected void buildExceptionExistingDerivedMapInfo(ExceptionMessageBuilder brMap<StringObjectderivedMap) {
        br.addItem("Existing DerivedMap");
        for (Entry<StringObjectentry : derivedMap.entrySet()) {
            final Object value = entry.getValue();
            br.addElement(entry.getKey() + " = " + (value != null ? value.getClass() : null));
        }
    }
    protected void throwDerivedPropertyValueNotFoundException(String tableDbNameString aliasName) { // embedded in optional
        final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
        br.addNotice("Not found the derived property value.");
        br.addItem("Advice");
        br.addElement("Please confirm the existence your property value.");
        br.addElement("Especially e.g. max(), sum(), ... might return null if no referrer data");
        br.addElement("For example:");
        br.addElement("  (x):");
        br.addElement("    String highestAlias = \"$HIGHEST_PURCHASE_PRICE\"");
        br.addElement("    cb.specify().derivedPurchaseList().max(purchaseCB -> {");
        br.addElement("        purchaseCB.specify().columnPurchasePrice();");
        br.addElement("    }, highestAlias);");
        br.addElement("    ...");
        br.addElement("    Member member = ...");
        br.addElement("    member.derived(highestAlias, Integer.class).alwaysPresent(...); // *NG");
        br.addElement("  (o):");
        br.addElement("    String highestAlias = \"$HIGHEST_PURCHASE_PRICE\"");
        br.addElement("    cb.specify().derivedPurchaseList().max(purchaseCB -> {");
        br.addElement("        purchaseCB.specify().columnPurchasePrice();");
        br.addElement("    }, highestAlias);");
        br.addElement("    ...");
        br.addElement("    Member member = ...");
        br.addElement("    member.derived(highestAlias, Integer.class).ifPresent(...); // OK");
        br.addElement("  (o):");
        br.addElement("    String highestAlias = \"$HIGHEST_PURCHASE_PRICE\"");
        br.addElement("    cb.specify().derivedPurchaseList().max(purchaseCB -> {");
        br.addElement("        purchaseCB.specify().columnPurchasePrice();");
        br.addElement("    }, highestAlias, op -> op.coalesce(0)); // *point");
        br.addElement("    ...");
        br.addElement("    Member member = ...");
        br.addElement("    member.derived(highestAlias, Integer.class).alwaysPresent(...); // OK");
        br.addItem("Table");
        br.addElement(tableDbName);
        br.addItem("DerivedProperty (Alias)");
        br.addElement(aliasName);
        // embedded in optional so no reference to derived map
        //buildExceptionExistingDerivedMapInfo(br, getDerivedMap());
        final String msg = br.buildExceptionMessage();
    }
    // ===================================================================================
    //                                                                      Basic Override
    //                                                                      ==============
    @Override
    public String toString() {
        return "derivedMap:" + ;
    }
New to GrepCode? Check out our FAQ X