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 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* 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.dv8tion.jda.api.utils.data;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.MapType;
import net.dv8tion.jda.api.exceptions.ParsingException;
import net.dv8tion.jda.api.utils.MiscUtil;
import net.dv8tion.jda.api.utils.data.etf.ExTermDecoder;
import net.dv8tion.jda.api.utils.data.etf.ExTermEncoder;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.Helpers;
import org.jetbrains.annotations.Contract;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.*;
import java.nio.ByteBuffer;
import java.time.OffsetDateTime;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.function.Function;
import java.util.function.UnaryOperator;
/**
* Represents a map of values used in communication with the Discord API.
*
*
Throws {@link java.lang.NullPointerException},
* if a parameter annotated with {@link javax.annotation.Nonnull} is provided with {@code null}.
*
*
This class is not Thread-Safe.
*/
public class DataObject implements SerializableData
{
private static final Logger log = LoggerFactory.getLogger(DataObject.class);
private static final ObjectMapper mapper;
private static final SimpleModule module;
private static final MapType mapType;
static
{
mapper = new ObjectMapper();
module = new SimpleModule();
module.addAbstractTypeMapping(Map.class, HashMap.class);
module.addAbstractTypeMapping(List.class, ArrayList.class);
mapper.registerModule(module);
mapType = mapper.getTypeFactory().constructRawMapType(HashMap.class);
}
protected final Map data;
protected DataObject(@Nonnull Map data)
{
this.data = data;
}
/**
* Creates a new empty DataObject, ready to be populated with values.
*
* @return An empty DataObject instance
*
* @see #put(String, Object)
*/
@Nonnull
public static DataObject empty()
{
return new DataObject(new HashMap<>());
}
/**
* Parses a JSON payload into a DataObject instance.
*
* @param data
* The correctly formatted JSON payload to parse
*
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the provided json is incorrectly formatted
*
* @return A DataObject instance for the provided payload
*/
@Nonnull
public static DataObject fromJson(@Nonnull byte[] data)
{
try
{
Map map = mapper.readValue(data, mapType);
return new DataObject(map);
}
catch (IOException ex)
{
throw new ParsingException(ex);
}
}
/**
* Parses a JSON payload into a DataObject instance.
*
* @param json
* The correctly formatted JSON payload to parse
*
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the provided json is incorrectly formatted
*
* @return A DataObject instance for the provided payload
*/
@Nonnull
public static DataObject fromJson(@Nonnull String json)
{
try
{
Map map = mapper.readValue(json, mapType);
return new DataObject(map);
}
catch (IOException ex)
{
throw new ParsingException(ex);
}
}
/**
* Parses a JSON payload into a DataObject instance.
*
* @param stream
* The correctly formatted JSON payload to parse
*
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the provided json is incorrectly formatted or an I/O error occurred
*
* @return A DataObject instance for the provided payload
*/
@Nonnull
public static DataObject fromJson(@Nonnull InputStream stream)
{
try
{
Map map = mapper.readValue(stream, mapType);
return new DataObject(map);
}
catch (IOException ex)
{
throw new ParsingException(ex);
}
}
/**
* Parses a JSON payload into a DataObject instance.
*
* @param stream
* The correctly formatted JSON payload to parse
*
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the provided json is incorrectly formatted or an I/O error occurred
*
* @return A DataObject instance for the provided payload
*/
@Nonnull
public static DataObject fromJson(@Nonnull Reader stream)
{
try
{
Map map = mapper.readValue(stream, mapType);
return new DataObject(map);
}
catch (IOException ex)
{
throw new ParsingException(ex);
}
}
/**
* Parses using {@link ExTermDecoder}.
* The provided data must start with the correct version header (131).
*
* @param data
* The data to decode
*
* @throws IllegalArgumentException
* If the provided data is null
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the provided ETF payload is incorrectly formatted or an I/O error occurred
*
* @return A DataObject instance for the provided payload
*
* @since 4.2.1
*/
@Nonnull
public static DataObject fromETF(@Nonnull byte[] data)
{
Checks.notNull(data, "Data");
try
{
Map map = ExTermDecoder.unpackMap(ByteBuffer.wrap(data));
return new DataObject(map);
}
catch (Exception ex)
{
log.error("Failed to parse ETF data {}", Arrays.toString(data), ex);
throw new ParsingException(ex);
}
}
/**
* Whether the specified key is present.
*
* @param key
* The key to check
*
* @return True, if the specified key is present
*/
public boolean hasKey(@Nonnull String key)
{
return data.containsKey(key);
}
/**
* Whether the specified key is missing or null
*
* @param key
* The key to check
*
* @return True, if the specified key is null or missing
*/
public boolean isNull(@Nonnull String key)
{
return data.get(key) == null;
}
/**
* Whether the specified key is of the specified type.
*
* @param key
* The key to check
* @param type
* The type to check
*
* @return True, if the type check is successful
*
* @see net.dv8tion.jda.api.utils.data.DataType#isType(Object) DataType.isType(Object)
*/
public boolean isType(@Nonnull String key, @Nonnull DataType type)
{
return type.isType(data.get(key));
}
/**
* Resolves a DataObject to a key.
*
* @param key
* The key to check for a value
*
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the type is incorrect or no value is present for the specified key
*
* @return The resolved instance of DataObject for the key
*/
@Nonnull
public DataObject getObject(@Nonnull String key)
{
return optObject(key).orElseThrow(() -> valueError(key, "DataObject"));
}
/**
* Resolves a DataObject to a key.
*
* @param key
* The key to check for a value
*
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the type is incorrect
*
* @return The resolved instance of DataObject for the key, wrapped in {@link java.util.Optional}
*/
@Nonnull
@SuppressWarnings("unchecked")
public Optional optObject(@Nonnull String key)
{
Map child = null;
try
{
child = (Map) get(Map.class, key);
}
catch (ClassCastException ex)
{
log.error("Unable to extract child data", ex);
}
return child == null ? Optional.empty() : Optional.of(new DataObject(child));
}
/**
* Resolves a DataArray to a key.
*
* @param key
* The key to check for a value
*
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the type is incorrect or no value is present for the specified key
*
* @return The resolved instance of DataArray for the key
*/
@Nonnull
public DataArray getArray(@Nonnull String key)
{
return optArray(key).orElseThrow(() -> valueError(key, "DataArray"));
}
/**
* Resolves a DataArray to a key.
*
* @param key
* The key to check for a value
*
* @throws net.dv8tion.jda.api.exceptions.ParsingException
* If the type is incorrect
*
* @return The resolved instance of DataArray for the key, wrapped in {@link java.util.Optional}
*/
@Nonnull
@SuppressWarnings("unchecked")
public Optional optArray(@Nonnull String key)
{
List