Newer
Older
/*
* _____________________________________________________________________________
*
* INAF - OATS National Institute for Astrophysics - Astronomical Observatory of
* Trieste INAF - IA2 Italian Center for Astronomical Archives
* _____________________________________________________________________________
*
* Copyright (C) 2016 Istituto Nazionale di Astrofisica
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License Version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package it.inaf.ia2.tsm;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
* Utility class containing some static methods to manage various operations
* with the TAP_SCHEMA entities.
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
public class TSMUtil {
private static final Logger LOG = LoggerFactory.getLogger(TSMUtil.class);
protected static List<String> sortStringsList(List<String> list) {
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
return list;
}
protected static <T extends ChildEntity> List<T> getChildrenByStatus(Iterable<T> entities, Status... statuses) {
List<T> ret = new ArrayList<>();
for (T child : entities) {
if (statuses == null || statuses.length == 0) {
ret.add(child);
}
} else {
for (Status status : statuses) {
if (child != null && child.getStatus().equals(status)) {
ret.add(child);
break;
}
}
}
}
return Collections.unmodifiableList(ret);
}
protected static <T extends ChildEntity> T getChild(Map<String, T> children, String childName, Status... statuses) {
T child = children.get(childName);
if (child == null) {
return null;
}
if (statuses == null || statuses.length == 0) {
return child;
}
for (Status status : statuses) {
if (child.getStatus().equals(status)) {
return child;
}
}
return null;
}
protected static <T extends ChildEntity> List<String> getAddableChildrenNames(Map<String, T> children) {
List<String> list = new ArrayList<>();
for (Map.Entry<String, T> entry : children.entrySet()) {
T entity = entry.getValue();
if (entity == null || entity.getStatus() == Status.LOADED) {
list.add(entry.getKey());
}
}
return Collections.unmodifiableList(list);
}
* This method is thought for avoiding various problem encountered using the
* standard {@code ResultSet} {@code getObject()} method.
* In particular:
* <ul>
* <li>In some cases the {@code getObject()} method (e.g. with
* {@code Integer.class}) returns 0 instead of null.
* </li>
* <li>
* Method
* {@code org.postgresql.jdbc4.Jdbc4ResultSet.getObject(int, Class<T>)} is
* not yet implemented.
* </li>
* </ul>
public static <T> T getObject(ResultSet rs, String key, Class<T> type) throws SQLException {
T ret;
if (type == String.class) {
ret = (T) rs.getString(key);
} else if (type == Integer.class) {
ret = (T) (Integer) rs.getInt(key);
} else if (type == Long.class) {
ret = (T) (Long) rs.getLong(key);
} else if (type == Boolean.class) {
ret = (T) (Boolean) rs.getBoolean(key);
} else {
throw new UnsupportedOperationException("Type " + type.getCanonicalName() + " not supported by " + TSMUtil.class.getCanonicalName() + " getObject() method");
if (rs.wasNull()) {
return null;
}
return ret;
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Utility class for joining a collection of object properties.
*/
public static class StringJoiner<T> {
private final Iterable<T> values;
private final String separator;
public StringJoiner(Iterable<T> values, String separator) {
this.values = values;
this.separator = separator;
}
public final String getString() {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (T value : values) {
if (!first) {
sb.append(separator);
}
sb.append(getStringValue(value));
first = false;
}
return sb.toString();
}
public String getStringValue(T value) {
if (value instanceof String) {
return (String) value;
}
return null;
}
}