1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 package org.apache.directory.mavibot.btree.managed; 21 22 23 import java.io.IOException; 24 25 import org.apache.directory.mavibot.btree.serializer.ElementSerializer; 26 27 28 /** 29 * A class storing either a key, or an offset to the key on the page's byte[] 30 * 31 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 32 * 33 * <K> The key type 34 */ 35 public class KeyHolder<K> 36 { 37 /** The deserialized key */ 38 private K key; 39 40 /** The ByteBuffer storing the key */ 41 private byte[] raw; 42 43 /** The Key serializer */ 44 private ElementSerializer<K> keySerializer; 45 46 47 /** 48 * Create a new KeyHolder instance 49 * @param keySerializer The KeySerializer instance 50 * @param key The key to store 51 */ 52 /* No Qualifier */KeyHolder( ElementSerializer<K> keySerializer, K key ) 53 { 54 this.key = key; 55 this.keySerializer = keySerializer; 56 raw = keySerializer.serialize( key ); 57 } 58 59 60 /** 61 * Create a new KeyHolder instance 62 * @param keySerializer The KeySerializer instance 63 * @param raw the bytes representing the serialized key 64 */ 65 /* No Qualifier */KeyHolder( ElementSerializer<K> keySerializer, byte[] raw ) 66 { 67 this.key = null; 68 this.keySerializer = keySerializer; 69 this.raw = raw; 70 } 71 72 73 /** 74 * @return the key 75 */ 76 public K getKey() 77 { 78 if ( key == null ) 79 { 80 try 81 { 82 key = keySerializer.fromBytes( raw ); 83 } 84 catch ( IOException ioe ) 85 { 86 // Nothing we can do here... 87 } 88 } 89 90 return key; 91 } 92 93 94 /** 95 * @param key the Key to store in into the KeyHolder 96 */ 97 public void setKey( K key ) 98 { 99 this.key = key; 100 raw = keySerializer.serialize( key ); 101 } 102 103 104 /** 105 * @return The internal serialized byte[] 106 */ 107 /* No qualifier */byte[] getRaw() 108 { 109 return raw; 110 } 111 112 113 /** 114 * @see Object#toString() 115 */ 116 public String toString() 117 { 118 StringBuilder sb = new StringBuilder(); 119 120 sb.append( "KeyHolder[" ); 121 122 if ( key != null ) 123 { 124 sb.append( key ); 125 sb.append( ", " ); 126 } 127 else 128 { 129 sb.append( "null," ); 130 } 131 132 if ( raw != null ) 133 { 134 sb.append( raw.length ); 135 } 136 else 137 { 138 sb.append( "null" ); 139 } 140 141 sb.append( "]" ); 142 143 return sb.toString(); 144 } 145 }