net.sf.mmm.util.pojo.descriptor.base.PojoProperty Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mmm-util-pojo Show documentation
Show all versions of mmm-util-pojo Show documentation
This project provides common utitlities to for POJOs.
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.util.pojo.descriptor.base;
import net.sf.mmm.util.exception.api.NlsIllegalArgumentException;
/**
* This class represents the property of a {@link net.sf.mmm.util.pojo.api.Pojo} .
* It can be a simple property such as {@code fooBar}, an indexed property such as {@code fooBar[42]}
* or a mapped property such as {@code fooBar['my.key']}.
* This class contains the logic to parse such property and gives structured access via the offered getters.
*
* @author Joerg Hohwiller (hohwille at users.sourceforge.net)
* @since 1.1.0
*/
public class PojoProperty {
private final String name;
private final Integer index;
private final String key;
/**
* The constructor.
*
* @param propertyName is the raw property-name.
*/
public PojoProperty(String propertyName) {
super();
int len = propertyName.length();
int startIndex = propertyName.indexOf('[');
if (startIndex == -1) {
this.name = propertyName;
this.index = null;
this.key = null;
} else {
this.name = propertyName.substring(0, startIndex);
if (propertyName.charAt(len - 1) != ']') {
throw new NlsIllegalArgumentException(propertyName);
}
char c = propertyName.charAt(startIndex + 1);
if (c == '\'') {
if ((propertyName.charAt(len - 2) != '\'') || (startIndex >= (len - 3))) {
throw new NlsIllegalArgumentException(propertyName);
}
this.index = null;
this.key = propertyName.substring(startIndex + 2, len - 2);
} else if ((c >= '0') && (c <= '9')) {
String indexString = propertyName.substring(startIndex + 1, len - 1);
this.index = Integer.valueOf(indexString);
this.key = null;
} else {
throw new NlsIllegalArgumentException(propertyName);
}
}
if (this.name.length() == 0) {
throw new NlsIllegalArgumentException(propertyName);
}
}
/**
* This method gets the plain name of the property.
* Examples:
*
*
* propertyName
* new {@link PojoProperty}(propertyName).{@link #getName()}
*
*
* fooBar
* fooBar
*
*
* foo[42]
* foo
*
*
* bar['key']
* bar
*
*
*
* @return the plain name of the property.
*/
public String getName() {
return this.name;
}
/**
* This method gets the optional index.
*
* @return the index or {@code null} if this {@link PojoProperty} does NOT represent an indexed property.
*/
public Integer getIndex() {
return this.index;
}
/**
* This method gets the optional key.
*
* @return the key of {@code null} if this {@link PojoProperty} does NOT represent a mapped property.
*/
public String getKey() {
return this.key;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy