001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.server.xdbm; 021 022 023import org.apache.directory.api.ldap.model.cursor.Tuple; 024import org.apache.directory.api.ldap.model.entry.Entry; 025 026 027/** 028 * An index id value pair based on a Tuple which can optionally reference the 029 * indexed Entry if one has already been loaded. 030 * 031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 032 * @param <K> The key stored in the Tuple, associated key for the object 033 * @param <I> The ID of the object 034 */ 035public class IndexEntry<K, I> 036{ 037 /** The referenced Entry if loaded from the store */ 038 private Entry entry; 039 040 /** The underlying Tuple */ 041 private final Tuple<K, I> tuple = new Tuple<>(); 042 043 044 /** 045 * Creates a ForwardIndexEntry instance 046 */ 047 public IndexEntry() 048 { 049 super(); 050 } 051 052 053 /** 054 * Sets the key value tuple represented by this ForwardIndexEntry, after having 055 * reset the IndexEntry content (the Entry will now be null) 056 * 057 * @param tuple the tuple for the ForwardIndexEntry 058 */ 059 public void setTuple( Tuple<K, I> tuple ) 060 { 061 // Clear the entry 062 entry = null; 063 064 // And inject the tuple key and value 065 this.tuple.setKey( tuple.getKey() ); 066 this.tuple.setValue( tuple.getValue() ); 067 } 068 069 070 /** 071 * {@inheritDoc} 072 */ 073 public I getId() 074 { 075 return tuple.getValue(); 076 } 077 078 079 /** 080 * {@inheritDoc} 081 */ 082 public K getKey() 083 { 084 return tuple.getKey(); 085 } 086 087 088 /** 089 * {@inheritDoc} 090 */ 091 public void setId( I id ) 092 { 093 tuple.setValue( id ); 094 } 095 096 097 /** 098 * {@inheritDoc} 099 */ 100 public void setKey( K value ) 101 { 102 tuple.setKey( value ); 103 } 104 105 106 /** 107 * {@inheritDoc} 108 */ 109 public Entry getEntry() 110 { 111 return entry; 112 } 113 114 115 /** 116 * {@inheritDoc} 117 */ 118 public void setEntry( Entry entry ) 119 { 120 this.entry = entry; 121 } 122 123 124 /** 125 * {@inheritDoc} 126 */ 127 public Tuple<K, I> getTuple() 128 { 129 return tuple; 130 } 131 132 133 /** 134 * {@inheritDoc} 135 */ 136 public void clear() 137 { 138 entry = null; 139 tuple.setKey( null ); 140 tuple.setValue( null ); 141 } 142 143 144 /** 145 * {@inheritDoc} 146 */ 147 public void copy( IndexEntry<K, I> entry ) 148 { 149 this.entry = entry.getEntry(); 150 tuple.setKey( entry.getKey() ); 151 tuple.setValue( entry.getId() ); 152 } 153 154 155 public int hashCode() 156 { 157 if ( getId() == null ) 158 { 159 return 0; 160 } 161 162 return getId().hashCode(); 163 } 164 165 166 public boolean equals( Object that ) 167 { 168 if ( that == this ) 169 { 170 return true; 171 } 172 173 if ( !( that instanceof IndexEntry ) ) 174 { 175 return false; 176 } 177 178 @SuppressWarnings("unchecked") 179 IndexEntry<K, I> thatIndexEntry = ( IndexEntry<K, I> ) that; 180 181 if ( thatIndexEntry.getId() == null ) 182 { 183 return getId() == null; 184 } 185 186 return thatIndexEntry.getId().equals( getId() ); 187 } 188 189 190 /** 191 * {@inheritDoc} 192 */ 193 public String toString() 194 { 195 StringBuilder buf = new StringBuilder(); 196 buf.append( "IndexEntry[ " ); 197 buf.append( tuple.getKey() ); 198 buf.append( ", " ); 199 buf.append( tuple.getValue() ); 200 buf.append( " ]" ); 201 202 return buf.toString(); 203 } 204}