1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.dbcp2.datasources; 19 20 import java.io.Serializable; 21 import java.util.Arrays; 22 23 import org.apache.commons.dbcp2.Utils; 24 25 /** 26 * A {@code char} array wrapper that does not reveal its contents inadvertently through toString(). In contrast to, for 27 * example, AtomicReference which toString()'s its contents. 28 * 29 * May contain null. 30 * 31 * @since 2.9.0 32 */ 33 final class CharArray implements Serializable { 34 35 private static final long serialVersionUID = 1L; 36 37 static final CharArray NULL = new CharArray((char[]) null); 38 39 private final char[] chars; 40 41 CharArray(final char[] chars) { 42 this.chars = Utils.clone(chars); 43 } 44 45 CharArray(final String string) { 46 this.chars = Utils.toCharArray(string); 47 } 48 49 /** 50 * Converts the value of char array as a String. 51 * 52 * @return value as a string, may be null. 53 */ 54 String asString() { 55 return Utils.toString(chars); 56 } 57 58 @Override 59 public boolean equals(final Object obj) { 60 if (this == obj) { 61 return true; 62 } 63 if (!(obj instanceof CharArray)) { 64 return false; 65 } 66 final CharArray other = (CharArray) obj; 67 return Arrays.equals(chars, other.chars); 68 } 69 70 /** 71 * Gets the value of char array. 72 * 73 * @return value, may be null. 74 */ 75 char[] get() { 76 return Utils.clone(chars); 77 } 78 79 @Override 80 public int hashCode() { 81 return Arrays.hashCode(chars); 82 } 83 84 }