blob: 514bc859beedc16d808cfd8c787f3b6c63f19af1 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2008, 2021 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.jcommons.ts.core.util;
import java.util.Map;
/**
* Utilities for implementations of {@link org.eclipse.statet.jcommons.ts.core.ToolCommandHandler}.
*/
public class ToolCommandHandlerUtils {
@SuppressWarnings("unchecked")
public static <C> C getCheckedData(final Map<String, Object> data, final String name,
final Class<C> clazz, final boolean required) {
final Object obj= data.get(name);
if (obj == null) {
if (required) {
throw new IllegalArgumentException("missing data entry: '" + name + '"');
}
return null;
}
if (!clazz.isInstance(obj)) {
throw new IllegalArgumentException("incompatible data entry: '" + name + "' (" + obj.getClass().getName() + ")");
}
return (C) obj;
}
@SuppressWarnings("unchecked")
public static <C> C getCheckedData(final Map<String, Object> data, final String name, final C defValue) {
final Object obj= data.get(name);
if (obj == null) {
return defValue;
}
if (!defValue.getClass().isInstance(obj)) {
throw new IllegalArgumentException("incompatible data entry: '" + name + "' (" + obj.getClass().getName() + ")");
}
return (C) obj;
}
private ToolCommandHandlerUtils() {}
}