001/* 002 * Copyright 2015-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2015-2020 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2015-2020 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.util.json; 037 038 039 040import java.io.Serializable; 041 042import com.unboundid.util.NotMutable; 043import com.unboundid.util.ThreadSafety; 044import com.unboundid.util.ThreadSafetyLevel; 045import com.unboundid.util.Validator; 046 047 048 049/** 050 * This class provides a simple data structure that represents a field in a 051 * JSON object, containing a name and a value. This is primarily intended as a 052 * convenience when programmatically constructing JSON objects. 053 */ 054@NotMutable() 055@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 056public final class JSONField 057 implements Serializable 058{ 059 /** 060 * The serial version UID for this serializable class. 061 */ 062 private static final long serialVersionUID = -1397826405959590851L; 063 064 065 066 // The value for this field. 067 private final JSONValue value; 068 069 // The name for this field. 070 private final String name; 071 072 073 074 /** 075 * Creates a new JSON field with the specified name and value. 076 * 077 * @param name The name for this field. It must not be {@code null}. 078 * @param value The value for this field. It must not be {@code null} 079 * (although it may be a {@link JSONNull} instance). 080 */ 081 public JSONField(final String name, final JSONValue value) 082 { 083 Validator.ensureNotNull(name); 084 Validator.ensureNotNull(value); 085 086 this.name = name; 087 this.value = value; 088 } 089 090 091 092 /** 093 * Creates a new JSON field with the specified name and a {@link JSONBoolean} 094 * value. 095 * 096 * @param name The name for this field. It must not be {@code null}. 097 * @param value The value for this field. It must not be {@code null}. 098 */ 099 public JSONField(final String name, final boolean value) 100 { 101 this(name, (value ? JSONBoolean.TRUE : JSONBoolean.FALSE)); 102 } 103 104 105 106 /** 107 * Creates a new JSON field with the specified name and a {@link JSONNumber} 108 * value. 109 * 110 * @param name The name for this field. It must not be {@code null}. 111 * @param value The value for this field. It must not be {@code null}. 112 */ 113 public JSONField(final String name, final long value) 114 { 115 this(name, new JSONNumber(value)); 116 } 117 118 119 120 /** 121 * Creates a new JSON field with the specified name and a {@link JSONNumber} 122 * value. 123 * 124 * @param name The name for this field. It must not be {@code null}. 125 * @param value The value for this field. It must not be {@code null}. 126 */ 127 public JSONField(final String name, final double value) 128 { 129 this(name, new JSONNumber(value)); 130 } 131 132 133 134 /** 135 * Creates a new JSON field with the specified name and a {@link JSONString} 136 * value. 137 * 138 * @param name The name for this field. It must not be {@code null}. 139 * @param value The value for this field. It must not be {@code null}. 140 */ 141 public JSONField(final String name, final String value) 142 { 143 this(name, new JSONString(value)); 144 } 145 146 147 148 /** 149 * Retrieves the name for this field. 150 * 151 * @return The name for this field. 152 */ 153 public String getName() 154 { 155 return name; 156 } 157 158 159 160 /** 161 * Retrieves the value for this field. 162 * 163 * @return The value for this field. 164 */ 165 public JSONValue getValue() 166 { 167 return value; 168 } 169 170 171 172 /** 173 * Retrieves a hash code for this JSON field. 174 * 175 * @return A hash code for this JSON field. 176 */ 177 @Override() 178 public int hashCode() 179 { 180 return name.hashCode() + value.hashCode(); 181 } 182 183 184 185 /** 186 * Indicates whether the provided object is considered equal to this JSON 187 * field. 188 * 189 * @param o The object for which to make the determination. 190 * 191 * @return {@code true} if the provided object is a JSON field with the same 192 * name and an equivalent value, or {@code false} if not. 193 */ 194 @Override() 195 public boolean equals(final Object o) 196 { 197 if (o == this) 198 { 199 return true; 200 } 201 202 if (o instanceof JSONField) 203 { 204 final JSONField f = (JSONField) o; 205 return (name.equals(f.name) && value.equals(f.value)); 206 } 207 208 return false; 209 } 210 211 212 213 /** 214 * Retrieves a string representation of this field. 215 * 216 * @return A string representation of this field. 217 */ 218 @Override() 219 public String toString() 220 { 221 final StringBuilder buffer = new StringBuilder(); 222 toString(buffer); 223 return buffer.toString(); 224 } 225 226 227 228 /** 229 * Appends a string representation of this field to the provided buffer. 230 * 231 * @param buffer The buffer to which the information should be appended. 232 */ 233 public void toString(final StringBuilder buffer) 234 { 235 JSONString.encodeString(name, buffer); 236 buffer.append(':'); 237 value.toString(buffer); 238 } 239}