001 /* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at 010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE 011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE. 012 * See the License for the specific language governing permissions 013 * and limitations under the License. 014 * 015 * When distributing Covered Code, include this CDDL HEADER in each 016 * file and include the License file at 017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable, 018 * add the following below this CDDL HEADER, with the fields enclosed 019 * by brackets "[]" replaced with your own identifying information: 020 * Portions Copyright [yyyy] [name of copyright owner] 021 * 022 * CDDL HEADER END 023 * 024 * 025 * Copyright 2008 Sun Microsystems, Inc. 026 */ 027 package org.opends.server.types; 028 029 030 031 import java.util.Arrays; 032 033 034 035 /** 036 * This class provides a data structure that holds a byte array but 037 * also includes the necessary {@code equals} and {@code hashCode} 038 * methods to make it suitable for use in maps. 039 */ 040 @org.opends.server.types.PublicAPI( 041 stability=org.opends.server.types.StabilityLevel.UNCOMMITTED, 042 mayInstantiate=false, 043 mayExtend=false, 044 mayInvoke=true) 045 public final class ByteArray 046 { 047 // The array that will be wrapped by this object. 048 private final byte[] array; 049 050 051 052 /** 053 * Creates a new {@code ByteArray} object that wraps the provided 054 * array. 055 * 056 * @param array The array to be wrapped with this 057 * {@code ByteArray}. 058 */ 059 public ByteArray(byte[] array) 060 { 061 this.array = array; 062 } 063 064 065 066 /** 067 * Retrieves the array wrapped by this {@code ByteArray} object. 068 * 069 * @return The array wrapped by this {@code ByteArray} object. 070 */ 071 public byte[] array() 072 { 073 return array; 074 } 075 076 077 078 /** 079 * Retrieves a hash code for this {@code ByteArray}. It will be the 080 * sum of all of the bytes contained in the wrapped array. 081 * 082 * @return A hash code for this {@code ByteArray}. 083 */ 084 public int hashCode() 085 { 086 int hashCode = 0; 087 for (int i=0; i < array.length; i++) 088 { 089 hashCode += array[i]; 090 } 091 092 return hashCode; 093 } 094 095 096 097 /** 098 * Indicates whether the provided object is equal to this 099 * {@code ByteArray}. In order for it to be considered equal, the 100 * provided object must be a non-null {@code ByteArray} object with 101 * a wrapped array containing the same bytes in the same order. 102 * 103 * @param o The object for which to make the determination. 104 * 105 * @return {@code true} if the provided object is a 106 * {@code ByteArray} whose content is equal to that of this 107 * {@code ByteArray}, or {@code false} if not. 108 */ 109 public boolean equals(Object o) 110 { 111 if (o == this) 112 { 113 return true; 114 } 115 116 if (o == null) 117 { 118 return false; 119 } 120 121 if (o instanceof ByteArray) 122 { 123 ByteArray ba = (ByteArray) o; 124 return Arrays.equals(array, ba.array); 125 } 126 else 127 { 128 return false; 129 } 130 } 131 } 132