Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2015 Google Inc.
*
* 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 com.google.cloud.dataflow.sdk.util;
import com.google.api.client.util.Data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
/**
* A collection of static methods for manipulating datastructure representations
* transferred via the Dataflow API.
*/
public final class Structs {
private Structs() {} // Non-instantiable
public static String getString(Map map, String name) throws Exception {
return getValue(map, name, String.class, "a string");
}
public static String getString(
Map map, String name, @Nullable String defaultValue)
throws Exception {
return getValue(map, name, String.class, "a string", defaultValue);
}
public static byte[] getBytes(Map map, String name) throws Exception {
@Nullable byte[] result = getBytes(map, name, null);
if (result == null) {
throw new ParameterNotFoundException(name, map);
}
return result;
}
@Nullable
public static byte[] getBytes(Map map, String name, @Nullable byte[] defaultValue)
throws Exception {
@Nullable String jsonString = getString(map, name, null);
if (jsonString == null) {
return defaultValue;
}
// TODO: Need to agree on a format for encoding bytes in
// a string that can be sent to the backend, over the cloud
// map task work API. base64 encoding seems pretty common. Switch to it?
return StringUtils.jsonStringToByteArray(jsonString);
}
public static Boolean getBoolean(Map map, String name) throws Exception {
return getValue(map, name, Boolean.class, "a boolean");
}
@Nullable
public static Boolean getBoolean(
Map map, String name, @Nullable Boolean defaultValue)
throws Exception {
return getValue(map, name, Boolean.class, "a boolean", defaultValue);
}
public static Long getLong(Map map, String name) throws Exception {
return getValue(map, name, Long.class, "an int");
}
@Nullable
public static Long getLong(Map map, String name, @Nullable Long defaultValue)
throws Exception {
return getValue(map, name, Long.class, "an int", defaultValue);
}
@Nullable
public static List getStrings(
Map map, String name, @Nullable List defaultValue)
throws Exception {
@Nullable Object value = map.get(name);
if (value == null) {
if (map.containsKey(name)) {
throw new IncorrectTypeException(name, map, "a string or a list");
}
return defaultValue;
}
if (Data.isNull(value)) {
// This is a JSON literal null. When represented as a list of strings,
// this is an empty list.
return Collections.emptyList();
}
@Nullable String singletonString = decodeValue(value, String.class);
if (singletonString != null) {
return Collections.singletonList(singletonString);
}
if (!(value instanceof List)) {
throw new IncorrectTypeException(name, map, "a string or a list");
}
@SuppressWarnings("unchecked")
List