net.oneandone.stool.configuration.Property Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of main Show documentation
Show all versions of main Show documentation
Stool's main component. Java Library, cli, setup code.
/**
* Copyright 1&1 Internet AG, https://github.com/1and1/
*
* 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 net.oneandone.stool.configuration;
import net.oneandone.inline.ArgumentException;
import net.oneandone.stool.extensions.Switch;
import net.oneandone.stool.util.Info;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** Handles Stool or Stage property. Converts between strings an objects and deals with reflection */
public class Property implements Info {
public final String name;
private final Field field;
private final String extension;
public Property(String name, Field field, String extension) {
this.name = name;
this.field = field;
this.extension = extension;
field.setAccessible(true);
}
public String get(StoolConfiguration configuration) {
return doGet(configuration);
}
public String get(StageConfiguration configuration) {
return doGet(configuration);
}
private String doGet(Object configuration) {
Object obj;
StringBuilder builder;
boolean first;
try {
obj = field.get(object(configuration));
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
}
if (obj instanceof List) {
builder = new StringBuilder();
first = true;
for (Object item : (List) obj) {
if (first) {
first = false;
} else {
builder.append(", ");
}
builder.append(item.toString());
}
return builder.toString();
} else if (obj instanceof Map) {
builder = new StringBuilder();
first = true;
for (Map.Entry