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

org.openapi4j.parser.model.AbsOpenApiSchema Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package org.openapi4j.parser.model;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.openapi4j.core.exception.EncodeException;
import org.openapi4j.core.util.TreeUtil;

import java.util.*;

import static org.openapi4j.core.util.TreeUtil.ENCODE_ERR_MSG;

public abstract class AbsOpenApiSchema> implements OpenApiSchema {
  protected static class Views {
    private Views() {
    }

    public static class Public {
      private Public() {
      }
    }

    public static class Internal extends Public {
      private Internal() {
      }
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public JsonNode toNode() throws EncodeException {

    try {
      byte[] content = TreeUtil.json
        .writerWithView(Views.Public.class)
        .writeValueAsBytes(this);

      return TreeUtil.json.readTree(content);
    } catch (Exception e) {
      throw new EncodeException(String.format(ENCODE_ERR_MSG, e.getMessage()));
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public String toString(EnumSet flags) throws EncodeException {
    ObjectMapper mapper
      = flags.contains(SerializationFlag.OUT_AS_YAML)
      ? TreeUtil.yaml
      : TreeUtil.json;

    try {
      return mapper
        .writerWithView(Views.Public.class)
        .writeValueAsString(this);
    } catch (Exception e) {
      throw new EncodeException(String.format(ENCODE_ERR_MSG, e.getMessage()));
    }
  }

  protected > T copyField(T original) {
    if (original != null) {
      return original.copy();
    }

    return null;
  }

  protected > Map copyMap(Map original) {
    if (original != null) {
      Map copy = new LinkedHashMap<>(original.size());
      for (Map.Entry element : original.entrySet()) {
        OpenApiSchema schema = element.getValue();
        if (schema != null) {
          copy.put(element.getKey(), schema.copy());
        } else {
          copy.put(element.getKey(), null);
        }
      }

      return copy;
    }

    return null;
  }

  protected  Map copySimpleMap(Map original) {
    if (original != null) {
      return new LinkedHashMap<>(original);
    }

    return null;
  }

  protected  Map mapPut(Map map, K key, V value) {
    if (map == null) {
      map = new LinkedHashMap<>();
    }
    map.put(key, value);

    return map;
  }

  protected  boolean mapHas(Map map, K key) {
    return map != null && map.containsKey(key);
  }

  protected  V mapGet(Map map, K key) {
    if (map == null) {
      return null;
    }
    return map.get(key);
  }

  protected  void mapRemove(Map map, K key) {
    if (map != null) {
      map.remove(key);
    }
  }

  protected > List copyList(List original) {
    if (original != null) {
      List copy = new ArrayList<>(original.size());
      for (T element : original) {
        copy.add(element.copy());
      }

      return copy;
    }

    return null;
  }

  protected  List copySimpleList(List original) {
    if (original != null) {
      return new ArrayList<>(original);
    }

    return null;
  }

  protected  List listAdd(List list, K value) {
    if (list == null) {
      list = new ArrayList<>();
    }
    list.add(value);

    return list;
  }

  protected  List listAdd(List list, int index, K value) {
    if (list == null) {
      list = new ArrayList<>();
    }
    list.add(index, value);

    return list;
  }

  protected  boolean listRemove(List list, K value) {
    if (list == null) return false;

    return list.remove(value);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy