1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.core.document.internal; 17 18 import java.util.List; 19 import java.util.Map; 20 import software.amazon.awssdk.annotations.Immutable; 21 import software.amazon.awssdk.annotations.SdkInternalApi; 22 import software.amazon.awssdk.core.SdkNumber; 23 import software.amazon.awssdk.core.document.Document; 24 import software.amazon.awssdk.core.document.DocumentVisitor; 25 import software.amazon.awssdk.core.document.VoidDocumentVisitor; 26 27 @SdkInternalApi 28 @Immutable 29 public final class NullDocument implements Document { 30 31 private static final long serialVersionUID = 1L; 32 33 /** 34 * Unwraps NullDocument as null. 35 * @return null 36 */ 37 @Override unwrap()38 public Object unwrap() { 39 return null; 40 } 41 42 /** 43 * {@inheritdoc} 44 */ 45 @Override asBoolean()46 public boolean asBoolean() { 47 throw new UnsupportedOperationException("A Document Null cannot be converted to a Boolean."); 48 } 49 50 /** 51 * {@inheritdoc} 52 */ 53 @Override asString()54 public String asString() { 55 throw new UnsupportedOperationException("A Document Null cannot be converted to a String."); 56 } 57 58 /** 59 * {@inheritdoc} 60 */ 61 @Override asNumber()62 public SdkNumber asNumber() { 63 throw new UnsupportedOperationException("A Document Null cannot be converted to a Number."); 64 } 65 66 /** 67 * {@inheritdoc} 68 */ 69 @Override asMap()70 public Map<String, Document> asMap() { 71 throw new UnsupportedOperationException("A Document Null cannot be converted to a Map."); 72 } 73 74 /** 75 * @return true ,since this is a Document Null. 76 */ 77 @Override isNull()78 public boolean isNull() { 79 return true; 80 } 81 82 /** 83 * {@inheritdoc} 84 */ 85 @Override asList()86 public List<Document> asList() { 87 throw new UnsupportedOperationException("A Document Null cannot be converted to a List."); 88 } 89 90 /** 91 * Accepts a visitor with the Document. 92 * @param <R> visitor return type. 93 * @param visitor Visitor to dispatch to. 94 * @return Returns the accepted result by calling visitNull of visitor. 95 */ 96 @Override accept(DocumentVisitor<? extends R> visitor)97 public <R> R accept(DocumentVisitor<? extends R> visitor) { 98 return visitor.visitNull(); 99 } 100 101 /** 102 * Accepts a visitor with the Document. Calls visitNull of visitor. 103 * @param visitor Visitor to dispatch to. 104 */ 105 @Override accept(VoidDocumentVisitor visitor)106 public void accept(VoidDocumentVisitor visitor) { 107 visitor.visitNull(); 108 } 109 110 @Override toString()111 public String toString() { 112 return "null"; 113 } 114 115 @Override hashCode()116 public int hashCode() { 117 return 0; 118 } 119 120 @Override equals(Object obj)121 public boolean equals(Object obj) { 122 if (this == obj) { 123 return true; 124 } 125 if (!(obj instanceof NullDocument)) { 126 return false; 127 } 128 NullDocument that = (NullDocument) obj; 129 return that.isNull() == this.isNull(); 130 } 131 } 132