All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.tomakehurst.wiremock.extension.responsetemplating.helpers.JsonData Maven / Gradle / Ivy

There is a newer version: 3.9.1
Show newest version
/*
 * Copyright (C) 2018-2023 Thomas Akehurst
 *
 * 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.github.tomakehurst.wiremock.extension.responsetemplating.helpers;

import com.github.tomakehurst.wiremock.common.Json;
import java.util.*;

public abstract class JsonData {

  protected abstract String toJsonString();

  @Override
  public String toString() {
    return toJsonString();
  }

  @SuppressWarnings("unchecked")
  static Object create(Object data) {
    if (data instanceof Map) {
      return new MapJsonData((Map) data);
    }

    if (data instanceof List) {
      return new ListJsonData((List) data);
    }

    return data;
  }

  protected final T data;

  public JsonData(T data) {
    this.data = data;
  }

  public static class MapJsonData extends JsonData>
      implements Map {

    @Override
    protected String toJsonString() {
      return Json.write(data);
    }

    public MapJsonData(Map data) {
      super(data);
    }

    @Override
    public int size() {
      return data.size();
    }

    @Override
    public boolean isEmpty() {
      return data.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
      return data.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
      return data.containsValue(value);
    }

    @Override
    public Object get(Object key) {
      return data.get(key);
    }

    @Override
    public Object remove(Object key) {
      return data.remove(key);
    }

    @Override
    public void clear() {
      data.clear();
    }

    @Override
    public Set keySet() {
      return data.keySet();
    }

    @Override
    public Collection values() {
      return data.values();
    }

    @Override
    public Set> entrySet() {
      return data.entrySet();
    }

    @Override
    public void putAll(Map m) {
      data.putAll(m);
    }

    @Override
    public Object put(String key, Object value) {
      return data.put(key, value);
    }
  }

  public static class ListJsonData extends JsonData> implements List {

    public ListJsonData(List data) {
      super(data);
    }

    public int size() {
      return data.size();
    }

    public boolean isEmpty() {
      return data.isEmpty();
    }

    public boolean contains(Object o) {
      return data.contains(o);
    }

    public Iterator iterator() {
      return data.iterator();
    }

    public Object[] toArray() {
      return data.toArray();
    }

    public  T[] toArray(T[] a) {
      return data.toArray(a);
    }

    public boolean add(Object o) {
      return data.add(o);
    }

    public boolean remove(Object o) {
      return data.remove(o);
    }

    public boolean containsAll(Collection c) {
      return data.containsAll(c);
    }

    public boolean addAll(Collection c) {
      return data.addAll(c);
    }

    public boolean addAll(int index, Collection c) {
      return data.addAll(index, c);
    }

    public boolean removeAll(Collection c) {
      return data.removeAll(c);
    }

    public boolean retainAll(Collection c) {
      return data.retainAll(c);
    }

    public void clear() {
      data.clear();
    }

    @Override
    public boolean equals(Object o) {
      return data.equals(o);
    }

    @Override
    public int hashCode() {
      return data.hashCode();
    }

    public Object get(int index) {
      return data.get(index);
    }

    public Object set(int index, Object element) {
      return data.set(index, element);
    }

    public void add(int index, Object element) {
      data.add(index, element);
    }

    public Object remove(int index) {
      return data.remove(index);
    }

    public int indexOf(Object o) {
      return data.indexOf(o);
    }

    public int lastIndexOf(Object o) {
      return data.lastIndexOf(o);
    }

    public ListIterator listIterator() {
      return data.listIterator();
    }

    public ListIterator listIterator(int index) {
      return data.listIterator(index);
    }

    public List subList(int fromIndex, int toIndex) {
      return data.subList(fromIndex, toIndex);
    }

    @Override
    protected String toJsonString() {
      return String.valueOf(data);
    }
  }
}