Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 5DCB7200D66 for ; Fri, 29 Dec 2017 16:53:32 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 5BBCB160C33; Fri, 29 Dec 2017 15:53:32 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 0A6C1160C34 for ; Fri, 29 Dec 2017 16:53:29 +0100 (CET) Received: (qmail 16295 invoked by uid 500); 29 Dec 2017 15:53:29 -0000 Mailing-List: contact commits-help@juneau.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@juneau.apache.org Delivered-To: mailing list commits@juneau.apache.org Received: (qmail 16266 invoked by uid 99); 29 Dec 2017 15:53:29 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 29 Dec 2017 15:53:29 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id A800D1A069B for ; Fri, 29 Dec 2017 15:53:28 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.222 X-Spam-Level: X-Spam-Status: No, score=-4.222 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.001, SPF_PASS=-0.001] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id C-bhGC-RHZVA for ; Fri, 29 Dec 2017 15:53:02 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with SMTP id 98CE85FB91 for ; Fri, 29 Dec 2017 15:52:50 +0000 (UTC) Received: (qmail 13330 invoked by uid 99); 29 Dec 2017 15:52:49 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 29 Dec 2017 15:52:49 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 5E658F17F8; Fri, 29 Dec 2017 15:52:48 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: jamesbognar@apache.org To: commits@juneau.incubator.apache.org Date: Fri, 29 Dec 2017 15:53:06 -0000 Message-Id: <4f81c06f2e994346a49c90475a480f7b@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [20/51] [partial] juneau-website git commit: Update javadocs. archived-at: Fri, 29 Dec 2017 15:53:32 -0000 http://git-wip-us.apache.org/repos/asf/juneau-website/blob/25d34ae9/content/site/apidocs/src-html/org/apache/juneau/PropertyStore.html ---------------------------------------------------------------------- diff --git a/content/site/apidocs/src-html/org/apache/juneau/PropertyStore.html b/content/site/apidocs/src-html/org/apache/juneau/PropertyStore.html index 8ea9c2d..e5df61d 100644 --- a/content/site/apidocs/src-html/org/apache/juneau/PropertyStore.html +++ b/content/site/apidocs/src-html/org/apache/juneau/PropertyStore.html @@ -391,369 +391,401 @@ 383 } 384 385 /** -386 * Returns an instance of the specified class or string property. +386 * Returns an instance of the specified class, string, or object property. 387 * -388 * @param key The property name. -389 * @param type The class type of the property. -390 * @param def -391 * The default value if the property doesn't exist. -392 * <br>Can either be an instance of <code>T</code>, or a <code>Class&lt;? <jk>extends</jk> T&gt;</code>. -393 * @return A new property instance. -394 */ -395 public <T> T getInstanceProperty(String key, Class<T> type, Object def) { -396 Property p = findProperty(key); -397 if (p != null) -398 return p.asInstance(type); -399 if (def == null) -400 return null; -401 if (def instanceof Class) { -402 T t = ClassUtils.newInstance(type, def); -403 if (t != null) -404 return t; -405 } -406 if (type.isInstance(def)) -407 return (T)def; -408 throw new ConfigException("Could not instantiate property ''{0}'' as type ''{1}'' with default value ''{2}''", key, type, def); -409 } -410 -411 /** -412 * Returns the specified property as an array of instantiated objects. -413 * -414 * @param key The property name. -415 * @param type The class type of the property. -416 * @param def The default object to return if the property doesn't exist. -417 * @return A new property instance. -418 */ -419 public <T> T[] getInstanceArrayProperty(String key, Class<T> type, T[] def) { -420 Property p = findProperty(key); -421 return p == null ? def : p.asInstanceArray(type); -422 } -423 -424 /** -425 * Returns the keys found in the specified property group. -426 * -427 * <p> -428 * The keys are NOT prefixed with group names. -429 * -430 * @param group The group name. -431 * @return The set of property keys, or an empty set if the group was not found. -432 */ -433 public Set<String> getPropertyKeys(String group) { -434 if (group == null) -435 return Collections.EMPTY_SET; -436 PropertyGroup g = groups.get(group); -437 return g == null ? Collections.EMPTY_SET : g.keySet(); -438 } -439 -440 @Override /* Object */ -441 public int hashCode() { -442 return hashCode; -443 } -444 -445 /** -446 * Returns a hashcode of this property store using only the specified group names. -447 * -448 * @param groups The names of the property groups to use in the calculation. -449 * @return The hashcode. -450 */ -451 public Integer hashCode(String...groups) { -452 HashCode c = new HashCode(); -453 for (String p : groups) -454 if (p != null) -455 c.add(p).add(this.groups.get(p)); -456 return c.get(); -457 } -458 -459 @Override /* Object */ -460 public boolean equals(Object o) { -461 if (this == o) -462 return true; -463 if (o instanceof PropertyStore) -464 return (this.groups.equals(((PropertyStore)o).groups)); -465 return false; -466 } -467 -468 /** -469 * Compares two property stores, but only based on the specified group names. -470 * -471 * @param ps The property store to compare to. -472 * @param groups The groups to compare. -473 * @return <jk>true</jk> if the two property stores are equal in the specified groups. -474 */ -475 public boolean equals(PropertyStore ps, String...groups) { -476 if (this == ps) -477 return true; -478 for (String p : groups) { -479 if (p != null) { -480 PropertyGroup pg1 = this.groups.get(p), pg2 = ps.groups.get(p); -481 if (pg1 == null && pg2 == null) -482 continue; -483 if (pg1 == null || pg2 == null) -484 return false; -485 if (! pg1.equals(pg2)) -486 return false; -487 } -488 } -489 return true; -490 } -491 -492 /** -493 * Used for debugging. -494 * -495 * <p> -496 * Allows property stores to be serialized to easy-to-read JSON objects. -497 * -498 * @param beanSession The bean session. -499 * @return The property groups. -500 */ -501 public Map<String,PropertyGroup> swap(BeanSession beanSession) { -502 return groups; +388 * <p> +389 * If instantiating a class, assumes the class has a no-arg constructor. +390 * Otherwise, throws a runtime exception. +391 * +392 * @param key The property name. +393 * @param type The class type of the property. +394 * @param def +395 * The default value if the property doesn't exist. +396 * <br>Can either be an instance of <code>T</code>, or a <code>Class&lt;? <jk>extends</jk> T&gt;</code>, or <jk>null</jk>. +397 * @return A new property instance. +398 */ +399 public <T> T getInstanceProperty(String key, Class<T> type, Object def) { +400 return getInstanceProperty(key, type, def, false); +401 } +402 +403 /** +404 * Returns an instance of the specified class, string, or object property. +405 * +406 * @param key The property name. +407 * @param type The class type of the property. +408 * @param def +409 * The default value if the property doesn't exist. +410 * <br>Can either be an instance of <code>T</code>, or a <code>Class&lt;? <jk>extends</jk> T&gt;</code>. +411 * @param allowNoArgs +412 * Look for no-arg constructors when instantiating a class. +413 * @param args +414 * Arguments to pass to the constructor. +415 * Constructors matching the arguments are always used before no-arg constructors. +416 * @return A new property instance. +417 */ +418 public <T> T getInstanceProperty(String key, Class<T> type, Object def, boolean allowNoArgs, Object...args) { +419 Property p = findProperty(key); +420 if (p != null) +421 return p.asInstance(type, allowNoArgs, args); +422 if (def == null) +423 return null; +424 if (def instanceof Class) +425 return ClassUtils.newInstance(type, def, allowNoArgs, args); +426 if (type.isInstance(def)) +427 return (T)def; +428 throw new ConfigException("Could not instantiate property ''{0}'' as type ''{1}'' with default value ''{2}''", key, type, def); +429 } +430 +431 /** +432 * Returns the specified property as an array of instantiated objects. +433 * +434 * @param key The property name. +435 * @param type The class type of the property. +436 * @param def The default object to return if the property doesn't exist. +437 * @return A new property instance. +438 */ +439 public <T> T[] getInstanceArrayProperty(String key, Class<T> type, T[] def) { +440 return getInstanceArrayProperty(key, type, def, false); +441 } +442 +443 /** +444 * Returns the specified property as an array of instantiated objects. +445 * +446 * @param key The property name. +447 * @param type The class type of the property. +448 * @param def The default object to return if the property doesn't exist. +449 * @param allowNoArgs +450 * Look for no-arg constructors when instantiating a class. +451 * @param args +452 * Arguments to pass to the constructor. +453 * Constructors matching the arguments are always used before no-arg constructors. +454 * @return A new property instance. +455 */ +456 public <T> T[] getInstanceArrayProperty(String key, Class<T> type, T[] def, boolean allowNoArgs, Object...args) { +457 Property p = findProperty(key); +458 return p == null ? def : p.asInstanceArray(type, allowNoArgs, args); +459 } +460 +461 /** +462 * Returns the keys found in the specified property group. +463 * +464 * <p> +465 * The keys are NOT prefixed with group names. +466 * +467 * @param group The group name. +468 * @return The set of property keys, or an empty set if the group was not found. +469 */ +470 public Set<String> getPropertyKeys(String group) { +471 if (group == null) +472 return Collections.EMPTY_SET; +473 PropertyGroup g = groups.get(group); +474 return g == null ? Collections.EMPTY_SET : g.keySet(); +475 } +476 +477 @Override /* Object */ +478 public int hashCode() { +479 return hashCode; +480 } +481 +482 /** +483 * Returns a hashcode of this property store using only the specified group names. +484 * +485 * @param groups The names of the property groups to use in the calculation. +486 * @return The hashcode. +487 */ +488 public Integer hashCode(String...groups) { +489 HashCode c = new HashCode(); +490 for (String p : groups) +491 if (p != null) +492 c.add(p).add(this.groups.get(p)); +493 return c.get(); +494 } +495 +496 @Override /* Object */ +497 public boolean equals(Object o) { +498 if (this == o) +499 return true; +500 if (o instanceof PropertyStore) +501 return (this.groups.equals(((PropertyStore)o).groups)); +502 return false; 503 } -504 -505 //------------------------------------------------------------------------------------------------------------------- -506 // PropertyGroup -507 //------------------------------------------------------------------------------------------------------------------- -508 -509 static class PropertyGroup { -510 final SortedMap<String,Property> properties; -511 private final int hashCode; -512 -513 PropertyGroup(Map<String,MutableProperty> properties) { -514 TreeMap<String,Property> m = new TreeMap<>(); -515 for (Map.Entry<String,MutableProperty> p : properties.entrySet()) -516 m.put(p.getKey(), p.getValue().build()); -517 this.properties = Collections.unmodifiableSortedMap(m); -518 this.hashCode = this.properties.hashCode(); -519 } -520 -521 PropertyGroupBuilder builder() { -522 return new PropertyGroupBuilder(properties); -523 } -524 -525 Property get(String key) { -526 return properties.get(key); -527 } -528 -529 @Override /* Object */ -530 public int hashCode() { -531 return hashCode; -532 } -533 -534 @Override /* Object */ -535 public boolean equals(Object o) { -536 if (o instanceof PropertyGroup) -537 return properties.equals(((PropertyGroup)o).properties); -538 return false; -539 } -540 -541 Set<String> keySet() { -542 return properties.keySet(); -543 } -544 -545 public Map<String,Property> swap(BeanSession beanSession) { -546 return properties; -547 } -548 } -549 -550 //------------------------------------------------------------------------------------------------------------------- -551 // Property -552 //------------------------------------------------------------------------------------------------------------------- -553 -554 static class Property { -555 private final String name; -556 final Object value; -557 private final int hashCode; -558 private final PropertyType type; -559 -560 Property(String name, Object value, PropertyType type) { -561 this.name = name; -562 this.value = value; -563 this.type = type; -564 this.hashCode = value.hashCode(); -565 } -566 -567 MutableProperty mutable() { -568 switch(type) { -569 case STRING: -570 case BOOLEAN: -571 case INTEGER: -572 case CLASS: -573 case OBJECT: return new MutableSimpleProperty(name, type, value); -574 case SET_STRING: -575 case SET_INTEGER: -576 case SET_CLASS: return new MutableSetProperty(name, type, value); -577 case LIST_STRING: -578 case LIST_INTEGER: -579 case LIST_CLASS: -580 case LIST_OBJECT: return new MutableListProperty(name, type, value); -581 case MAP_STRING: -582 case MAP_INTEGER: -583 case MAP_CLASS: -584 case MAP_OBJECT: return new MutableMapProperty(name, type, value); -585 } -586 throw new ConfigException("Invalid type specified: ''{0}''", type); -587 } -588 -589 public <T> T as(Class<T> c) { -590 Class<?> c2 = ClassUtils.getPrimitiveWrapper(c); -591 if (c2 != null) -592 c = (Class<T>)c2; -593 if (c.isInstance(value)) -594 return (T)value; -595 if (c.isArray() && value instanceof Collection) -596 return (T)asArray(c.getComponentType()); -597 if (type == STRING) { -598 T t = ClassUtils.fromString(c, value.toString()); -599 if (t != null) -600 return t; -601 } -602 throw new ConfigException("Invalid property conversion ''{0}'' to ''{1}'' on property ''{2}''", type, c, name); -603 } -604 -605 public <T> T[] asArray(Class<T> eType) { -606 if (value instanceof Collection) { -607 Collection<?> l = (Collection<?>)value; -608 Object t = Array.newInstance(eType, l.size()); -609 int i = 0; -610 for (Object o : l) { -611 Object o2 = null; -612 if (eType.isInstance(o)) -613 o2 = o; -614 else if (type == SET_STRING || type == LIST_STRING) { -615 o2 = ClassUtils.fromString(eType, o.toString()); -616 if (o2 == null) -617 throw new ConfigException("Invalid property conversion ''{0}'' to ''{1}[]'' on property ''{2}''", type, eType, name); -618 } else { -619 throw new ConfigException("Invalid property conversion ''{0}'' to ''{1}[]'' on property ''{2}''", type, eType, name); -620 } -621 Array.set(t, i++, o2); -622 } -623 return (T[])t; -624 } -625 throw new ConfigException("Invalid property conversion ''{0}'' to ''{1}[]'' on property ''{2}''", type, eType, name); -626 } -627 -628 public <T> Set<T> asSet(Class<T> eType) { -629 if (type == SET_STRING && eType == String.class || type == SET_INTEGER && eType == Integer.class || type == SET_CLASS && eType == Class.class) { -630 return (Set<T>)value; -631 } else if (type == SET_STRING) { -632 Set<T> s = new LinkedHashSet<>(); -633 for (Object o : (Set<?>)value) { -634 T t = ClassUtils.fromString(eType, o.toString()); -635 if (t == null) -636 throw new ConfigException("Invalid property conversion ''{0}'' to ''Set<{1}>'' on property ''{2}''", type, eType, name); -637 s.add(t); -638 } -639 return Collections.unmodifiableSet(s); -640 } else { -641 throw new ConfigException("Invalid property conversion ''{0}'' to ''Set<{1}>'' on property ''{2}''", type, eType, name); -642 } -643 } -644 -645 public <T> List<T> asList(Class<T> eType) { -646 if (type == LIST_STRING && eType == String.class || type == LIST_INTEGER && eType == Integer.class || type == LIST_CLASS && eType == Class.class || type == LIST_OBJECT) { -647 return (List<T>)value; -648 } else if (type == PropertyType.LIST_STRING) { -649 List<T> l = new ArrayList<>(); -650 for (Object o : (List<?>)value) { -651 T t = ClassUtils.fromString(eType, o.toString()); -652 if (t == null) -653 throw new ConfigException("Invalid property conversion ''{0}'' to ''List<{1}>'' on property ''{2}''", type, eType, name); -654 l.add(t); -655 } -656 return Collections.unmodifiableList(l); -657 } else { -658 throw new ConfigException("Invalid property conversion ''{0}'' to ''List<{1}>'' on property ''{2}''", type, eType, name); -659 } -660 } -661 -662 public <T> Map<String,T> asMap(Class<T> eType) { -663 if (type == MAP_STRING && eType == String.class || type == MAP_INTEGER && eType == Integer.class || type == MAP_CLASS && eType == Class.class || type == MAP_OBJECT) { -664 return (Map<String,T>)value; -665 } else if (type == MAP_STRING) { -666 Map<String,T> m = new LinkedHashMap<>(); -667 for (Map.Entry<String,String> e : ((Map<String,String>)value).entrySet()) { -668 T t = ClassUtils.fromString(eType, e.getValue()); -669 if (t == null) -670 throw new ConfigException("Invalid property conversion ''{0}'' to ''Map<String,{1}>'' on property ''{2}''", type, eType, name); -671 m.put(e.getKey(), t); -672 } -673 return Collections.unmodifiableMap(m); -674 } else { -675 throw new ConfigException("Invalid property conversion ''{0}'' to ''Map<String,{1}>'' on property ''{2}''", type, eType, name); -676 } -677 } -678 -679 public <T> T asInstance(Class<T> iType) { -680 T t = null; -681 if (type == STRING) -682 t = ClassUtils.fromString(iType, value.toString()); -683 else if (type == OBJECT && iType.isInstance(value)) -684 t = (T)value; -685 else if (type == CLASS) -686 t = ClassUtils.newInstance(iType, value); -687 if (t != null) -688 return t; -689 throw new ConfigException("Invalid property instantiation ''{0}'' to ''{1}'' on property ''{2}''", type, iType, name); -690 } -691 -692 public <T> T[] asInstanceArray(Class<T> eType) { -693 if (value instanceof Collection) { -694 Collection<?> l = (Collection<?>)value; -695 Object t = Array.newInstance(eType, l.size()); -696 int i = 0; -697 for (Object o : l) { -698 Object o2 = null; -699 if (eType.isInstance(o)) -700 o2 = o; -701 else if (type == SET_STRING || type == LIST_STRING) -702 o2 = ClassUtils.fromString(eType, o.toString()); -703 else if (type == SET_CLASS || type == LIST_CLASS) -704 o2 = ClassUtils.newInstance(eType, o); -705 if (o2 == null) -706 throw new ConfigException("Invalid property conversion ''{0}'' to ''{1}[]'' on property ''{2}''", type, eType, name); -707 Array.set(t, i++, o2); -708 } -709 return (T[])t; -710 } -711 throw new ConfigException("Invalid property conversion ''{0}'' to ''{1}[]'' on property ''{2}''", type, eType, name); -712 } -713 -714 @Override /* Object */ -715 public int hashCode() { -716 return hashCode; -717 } -718 -719 @Override /* Object */ -720 public boolean equals(Object o) { -721 if (o instanceof Property) -722 return ((Property)o).value.equals(value); -723 return false; -724 } -725 -726 public Object swap(BeanSession beanSession) { -727 return value; -728 } -729 } -730 -731 //------------------------------------------------------------------------------------------------------------------- -732 // Utility methods -733 //------------------------------------------------------------------------------------------------------------------- -734 -735 private static String group(String key) { -736 if (key == null || key.indexOf('.') == -1 || key.charAt(key.length()-1) == '.') -737 throw new ConfigException("Invalid property name specified: ''{0}''", key); -738 String g = key.substring(0, key.indexOf('.')); -739 if (g.isEmpty()) -740 throw new ConfigException("Invalid property name specified: ''{0}''", key); -741 return g; -742 } -743 -744 @Override /* Object */ -745 public String toString() { -746 return JsonSerializer.DEFAULT_LAX.toString(this); -747 } -748} +504 +505 /** +506 * Compares two property stores, but only based on the specified group names. +507 * +508 * @param ps The property store to compare to. +509 * @param groups The groups to compare. +510 * @return <jk>true</jk> if the two property stores are equal in the specified groups. +511 */ +512 public boolean equals(PropertyStore ps, String...groups) { +513 if (this == ps) +514 return true; +515 for (String p : groups) { +516 if (p != null) { +517 PropertyGroup pg1 = this.groups.get(p), pg2 = ps.groups.get(p); +518 if (pg1 == null && pg2 == null) +519 continue; +520 if (pg1 == null || pg2 == null) +521 return false; +522 if (! pg1.equals(pg2)) +523 return false; +524 } +525 } +526 return true; +527 } +528 +529 /** +530 * Used for debugging. +531 * +532 * <p> +533 * Allows property stores to be serialized to easy-to-read JSON objects. +534 * +535 * @param beanSession The bean session. +536 * @return The property groups. +537 */ +538 public Map<String,PropertyGroup> swap(BeanSession beanSession) { +539 return groups; +540 } +541 +542 //------------------------------------------------------------------------------------------------------------------- +543 // PropertyGroup +544 //------------------------------------------------------------------------------------------------------------------- +545 +546 static class PropertyGroup { +547 final SortedMap<String,Property> properties; +548 private final int hashCode; +549 +550 PropertyGroup(Map<String,MutableProperty> properties) { +551 TreeMap<String,Property> m = new TreeMap<>(); +552 for (Map.Entry<String,MutableProperty> p : properties.entrySet()) +553 m.put(p.getKey(), p.getValue().build()); +554 this.properties = Collections.unmodifiableSortedMap(m); +555 this.hashCode = this.properties.hashCode(); +556 } +557 +558 PropertyGroupBuilder builder() { +559 return new PropertyGroupBuilder(properties); +560 } +561 +562 Property get(String key) { +563 return properties.get(key); +564 } +565 +566 @Override /* Object */ +567 public int hashCode() { +568 return hashCode; +569 } +570 +571 @Override /* Object */ +572 public boolean equals(Object o) { +573 if (o instanceof PropertyGroup) +574 return properties.equals(((PropertyGroup)o).properties); +575 return false; +576 } +577 +578 Set<String> keySet() { +579 return properties.keySet(); +580 } +581 +582 public Map<String,Property> swap(BeanSession beanSession) { +583 return properties; +584 } +585 } +586 +587 //------------------------------------------------------------------------------------------------------------------- +588 // Property +589 //------------------------------------------------------------------------------------------------------------------- +590 +591 static class Property { +592 private final String name; +593 final Object value; +594 private final int hashCode; +595 private final PropertyType type; +596 +597 Property(String name, Object value, PropertyType type) { +598 this.name = name; +599 this.value = value; +600 this.type = type; +601 this.hashCode = value.hashCode(); +602 } +603 +604 MutableProperty mutable() { +605 switch(type) { +606 case STRING: +607 case BOOLEAN: +608 case INTEGER: +609 case CLASS: +610 case OBJECT: return new MutableSimpleProperty(name, type, value); +611 case SET_STRING: +612 case SET_INTEGER: +613 case SET_CLASS: return new MutableSetProperty(name, type, value); +614 case LIST_STRING: +615 case LIST_INTEGER: +616 case LIST_CLASS: +617 case LIST_OBJECT: return new MutableListProperty(name, type, value); +618 case MAP_STRING: +619 case MAP_INTEGER: +620 case MAP_CLASS: +621 case MAP_OBJECT: return new MutableMapProperty(name, type, value); +622 } +623 throw new ConfigException("Invalid type specified: ''{0}''", type); +624 } +625 +626 public <T> T as(Class<T> c) { +627 Class<?> c2 = ClassUtils.getPrimitiveWrapper(c); +628 if (c2 != null) +629 c = (Class<T>)c2; +630 if (c.isInstance(value)) +631 return (T)value; +632 if (c.isArray() && value instanceof Collection) +633 return (T)asArray(c.getComponentType()); +634 if (type == STRING) { +635 T t = ClassUtils.fromString(c, value.toString()); +636 if (t != null) +637 return t; +638 } +639 throw new ConfigException("Invalid property conversion ''{0}'' to ''{1}'' on property ''{2}''", type, c, name); +640 } +641 +642 public <T> T[] asArray(Class<T> eType) { +643 if (value instanceof Collection) { +644 Collection<?> l = (Collection<?>)value; +645 Object t = Array.newInstance(eType, l.size()); +646 int i = 0; +647 for (Object o : l) { +648 Object o2 = null; +649 if (eType.isInstance(o)) +650 o2 = o; +651 else if (type == SET_STRING || type == LIST_STRING) { +652 o2 = ClassUtils.fromString(eType, o.toString()); +653 if (o2 == null) +654 throw new ConfigException("Invalid property conversion ''{0}'' to ''{1}[]'' on property ''{2}''", type, eType, name); +655 } else { +656 throw new ConfigException("Invalid property conversion ''{0}'' to ''{1}[]'' on property ''{2}''", type, eType, name); +657 } +658 Array.set(t, i++, o2); +659 } +660 return (T[])t; +661 } +662 throw new ConfigException("Invalid property conversion ''{0}'' to ''{1}[]'' on property ''{2}''", type, eType, name); +663 } +664 +665 public <T> Set<T> asSet(Class<T> eType) { +666 if (type == SET_STRING && eType == String.class || type == SET_INTEGER && eType == Integer.class || type == SET_CLASS && eType == Class.class) { +667 return (Set<T>)value; +668 } else if (type == SET_STRING) { +669 Set<T> s = new LinkedHashSet<>(); +670 for (Object o : (Set<?>)value) { +671 T t = ClassUtils.fromString(eType, o.toString()); +672 if (t == null) +673 throw new ConfigException("Invalid property conversion ''{0}'' to ''Set<{1}>'' on property ''{2}''", type, eType, name); +674 s.add(t); +675 } +676 return Collections.unmodifiableSet(s); +677 } else { +678 throw new ConfigException("Invalid property conversion ''{0}'' to ''Set<{1}>'' on property ''{2}''", type, eType, name); +679 } +680 } +681 +682 public <T> List<T> asList(Class<T> eType) { +683 if (type == LIST_STRING && eType == String.class || type == LIST_INTEGER && eType == Integer.class || type == LIST_CLASS && eType == Class.class || type == LIST_OBJECT) { +684 return (List<T>)value; +685 } else if (type == PropertyType.LIST_STRING) { +686 List<T> l = new ArrayList<>(); +687 for (Object o : (List<?>)value) { +688 T t = ClassUtils.fromString(eType, o.toString()); +689 if (t == null) +690 throw new ConfigException("Invalid property conversion ''{0}'' to ''List<{1}>'' on property ''{2}''", type, eType, name); +691 l.add(t); +692 } +693 return Collections.unmodifiableList(l); +694 } else { +695 throw new ConfigException("Invalid property conversion ''{0}'' to ''List<{1}>'' on property ''{2}''", type, eType, name); +696 } +697 } +698 +699 public <T> Map<String,T> asMap(Class<T> eType) { +700 if (type == MAP_STRING && eType == String.class || type == MAP_INTEGER && eType == Integer.class || type == MAP_CLASS && eType == Class.class || type == MAP_OBJECT) { +701 return (Map<String,T>)value; +702 } else if (type == MAP_STRING) { +703 Map<String,T> m = new LinkedHashMap<>(); +704 for (Map.Entry<String,String> e : ((Map<String,String>)value).entrySet()) { +705 T t = ClassUtils.fromString(eType, e.getValue()); +706 if (t == null) +707 throw new ConfigException("Invalid property conversion ''{0}'' to ''Map<String,{1}>'' on property ''{2}''", type, eType, name); +708 m.put(e.getKey(), t); +709 } +710 return Collections.unmodifiableMap(m); +711 } else { +712 throw new ConfigException("Invalid property conversion ''{0}'' to ''Map<String,{1}>'' on property ''{2}''", type, eType, name); +713 } +714 } +715 +716 public <T> T asInstance(Class<T> iType, boolean allowNoArgs, Object...args) { +717 if (type == STRING) +718 return ClassUtils.fromString(iType, value.toString()); +719 else if (type == OBJECT || type == CLASS) +720 return ClassUtils.newInstance(iType, value, allowNoArgs, args); +721 throw new ConfigException("Invalid property instantiation ''{0}'' to ''{1}'' on property ''{2}''", type, iType, name); +722 } +723 +724 public <T> T[] asInstanceArray(Class<T> eType, boolean allowNoArgs, Object...args) { +725 if (value instanceof Collection) { +726 Collection<?> l = (Collection<?>)value; +727 Object t = Array.newInstance(eType, l.size()); +728 int i = 0; +729 for (Object o : l) { +730 Object o2 = null; +731 if (eType.isInstance(o)) +732 o2 = o; +733 else if (type == SET_STRING || type == LIST_STRING) +734 o2 = ClassUtils.fromString(eType, o.toString()); +735 else if (type == SET_CLASS || type == LIST_CLASS || type == LIST_OBJECT) +736 o2 = ClassUtils.newInstance(eType, o, allowNoArgs, args); +737 if (o2 == null) +738 throw new ConfigException("Invalid property conversion ''{0}'' to ''{1}[]'' on property ''{2}''", type, eType, name); +739 Array.set(t, i++, o2); +740 } +741 return (T[])t; +742 } +743 throw new ConfigException("Invalid property conversion ''{0}'' to ''{1}[]'' on property ''{2}''", type, eType, name); +744 } +745 +746 @Override /* Object */ +747 public int hashCode() { +748 return hashCode; +749 } +750 +751 @Override /* Object */ +752 public boolean equals(Object o) { +753 if (o instanceof Property) +754 return ((Property)o).value.equals(value); +755 return false; +756 } +757 +758 public Object swap(BeanSession beanSession) { +759 return value; +760 } +761 } +762 +763 //------------------------------------------------------------------------------------------------------------------- +764 // Utility methods +765 //------------------------------------------------------------------------------------------------------------------- +766 +767 private static String group(String key) { +768 if (key == null || key.indexOf('.') == -1 || key.charAt(key.length()-1) == '.') +769 throw new ConfigException("Invalid property name specified: ''{0}''", key); +770 String g = key.substring(0, key.indexOf('.')); +771 if (g.isEmpty()) +772 throw new ConfigException("Invalid property name specified: ''{0}''", key); +773 return g; +774 } +775 +776 @Override /* Object */ +777 public String toString() { +778 return JsonSerializer.DEFAULT_LAX.toString(this); +779 } +780}