Apache-Commons

Die Artikelserie „Common uses of Apache Commons“ zeigt Beispiele für den Einsatz von Apache Commons. Eine Sammlung von Klassen, die oft benötigte Aufgaben übernehmen können.

StringUtils

  • IsEmpty/IsBlank – checks if a String contains text
  • Trim/Strip – removes leading and trailing whitespace
  • Split/Join – splits a String into an array of substrings and vice versa
  • IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable – checks the characters in a String

BeanUtils

  • copyProperties(Object dest, Object orig) Copy property values from the origin bean to the destination bean for all cases where the property names are the same.
  • describe(Object bean) Return the entire set of properties for which the specified bean provides a read method.
  • populate(Object bean, Map properties) Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs.

CollectionUtils

  • intersection(java.util.Collection a, java.util.Collection b) Returns a Collection containing the intersection of the given Collections.
  • isEmpty(java.util.Collection coll) Null-safe check if the specified collection is empty.
  • subtract(java.util.Collection a, java.util.Collection b) Returns a new Collection containing a-b.

ReflectionToStringBuilder

[code lang=“java“]public String toString() {
return ReflectionToStringBuilder.toString(this);
}[/code]

ToStringBuilder

[code lang=“java“]public String toString() {
return new ToStringBuilder(this).
append(„Name“, name).
append(„Age“, age).toString();
}[/code]

EqualsBuilder

[code lang=“java“]public boolean equals(Object that) {
return EqualsBuilder.reflectionEquals(this, that);
}[/code]

[code lang=“java“]public boolean equals(Object that) {
EqualsBuilder builder = new EqualsBuilder();
return builder.append(this.field1, that.field1)
.append(this.field2, that.field2)
.isEquals();
}[/code]

CompareToBuilder

[code lang=“java“]public int compareTo(Object rhs) {
return CompareToBuilder.reflectionCompare(this, rhs);
}[/code]

[code lang=“java“]public int compare(Object this, Object rhs) {
CompareToBuilder builder = new CompareToBuilder();
return builder.append(this.name, rhs.name).toComparison();
}[/code]

HashCodeBuilder

[code lang=“java“]public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}[/code]

[code lang=“java“]public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder();
return builder.append(this.value1)
.append(this.value2)
.toHashCode();
}[/code]

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.