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

org.apache.avro.JsonProperties Maven / Gradle / Ivy

There is a newer version: 1.12.0
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.avro;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.io.IOException;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.node.TextNode;

/** Base class for objects that have Json-valued properties. */
public abstract class JsonProperties {
  Map props = new LinkedHashMap(1);

  private Set reserved;

  JsonProperties(Set reserved) {
    this.reserved = reserved;
  }

  /**
   * Returns the value of the named, string-valued property in this schema.
   * Returns null if there is no string-valued property with that name.
   */
  public String getProp(String name) {
    JsonNode value = getJsonProp(name);
    return value != null && value.isTextual() ? value.getTextValue() : null;
  }

  /**
   * Returns the value of the named property in this schema.
   * Returns null if there is no property with that name.
   */
  public synchronized JsonNode getJsonProp(String name) {
    return props.get(name);
  }

  /**
   * Adds a property with the given name name and
   * value value. Neither name nor value can be
   * null. It is illegal to add a property if another with
   * the same name but different value already exists in this schema.
   * 
   * @param name The name of the property to add
   * @param value The value for the property to add
   */
  public void addProp(String name, String value) {
    addProp(name, TextNode.valueOf(value));
  }

  /**
   * Adds a property with the given name name and
   * value value. Neither name nor value can be
   * null. It is illegal to add a property if another with
   * the same name but different value already exists in this schema.
   * 
   * @param name The name of the property to add
   * @param value The value for the property to add
   */
  public synchronized void addProp(String name, JsonNode value) {
    if (reserved.contains(name))
      throw new AvroRuntimeException("Can't set reserved property: " + name);
      
    if (value == null)
      throw new AvroRuntimeException("Can't set a property to null: " + name);
    
    JsonNode old = props.get(name);
    if (old == null)
      props.put(name, value);
    else if (!old.equals(value))
      throw new AvroRuntimeException("Can't overwrite property: " + name);
  }

  /** Return the defined properties that have string values. */
  @Deprecated public Map getProps() {
    Map result = new LinkedHashMap();
    for (Map.Entry e : props.entrySet())
      if (e.getValue().isTextual())
        result.put(e.getKey(), e.getValue().getTextValue());
    return result;
  }

  /** Convert a map of string-valued properties to Json properties. */
  Map jsonProps(Map stringProps) {
    Map result = new LinkedHashMap();
    for (Map.Entry e : stringProps.entrySet())
      result.put(e.getKey(), TextNode.valueOf(e.getValue()));
    return result;
  }

  /** Return the defined properties as an unmodifieable Map. */
  public Map getJsonProps() {
    return Collections.unmodifiableMap(props);
  }

  void writeProps(JsonGenerator gen) throws IOException {
    for (Map.Entry e : props.entrySet())
      gen.writeObjectField(e.getKey(), e.getValue());
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy