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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2015 ForgeRock AS
025 */
026package org.opends.guitools.controlpanel.datamodel;
027
028import java.util.HashSet;
029import java.util.LinkedHashSet;
030import java.util.Set;
031
032import org.opends.server.admin.std.meta.BackendIndexCfgDefn;
033import org.opends.server.util.RemoveOnceNewConfigFrameworkIsUsed;
034
035/**
036 * Defines the set of values for the index type and provides adaptors to convert
037 * from/to corresponding configuration classes.
038 */
039@RemoveOnceNewConfigFrameworkIsUsed
040public enum IndexTypeDescriptor
041{
042  /**
043   * This index type is used to improve the efficiency of searches using
044   * approximate matching search filters.
045   */
046  APPROXIMATE(BackendIndexCfgDefn.IndexType.APPROXIMATE,
047      org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType.APPROXIMATE),
048
049  /**
050   * This index type is used to improve the efficiency of searches using
051   * equality search filters.
052   */
053  EQUALITY(BackendIndexCfgDefn.IndexType.EQUALITY,
054      org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType.EQUALITY),
055
056  /**
057   * This index type is used to improve the efficiency of searches using
058   * extensible matching search filters.
059   */
060  EXTENSIBLE(BackendIndexCfgDefn.IndexType.EXTENSIBLE,
061      org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType.EXTENSIBLE),
062
063  /**
064   * This index type is used to improve the efficiency of searches using
065   * "greater than or equal to" or "less then or equal to" search filters.
066   */
067  ORDERING(BackendIndexCfgDefn.IndexType.ORDERING,
068      org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType.ORDERING),
069
070  /**
071   * This index type is used to improve the efficiency of searches using the
072   * presence search filters.
073   */
074  PRESENCE(BackendIndexCfgDefn.IndexType.PRESENCE,
075      org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType.PRESENCE),
076
077  /**
078   * This index type is used to improve the efficiency of searches using
079   * substring search filters.
080   */
081  SUBSTRING(BackendIndexCfgDefn.IndexType.SUBSTRING,
082      org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType.SUBSTRING);
083
084  private final BackendIndexCfgDefn.IndexType oldConfigBackendIndexType;
085  private final org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType backendIndexType;
086
087  private IndexTypeDescriptor(final BackendIndexCfgDefn.IndexType oldConfigBackendIndexType,
088      final org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType backendIndexType)
089  {
090    this.oldConfigBackendIndexType = oldConfigBackendIndexType;
091    this.backendIndexType = backendIndexType;
092  }
093
094  /**
095   * Convert the index type to the equivalent
096   * {@code BackendIndexCfgDefn.IndexType}.
097   *
098   * @return The index type to the equivalent
099   *         {@code BackendIndexCfgDefn.IndexType}
100   */
101  public BackendIndexCfgDefn.IndexType toBackendIndexType()
102  {
103    return oldConfigBackendIndexType;
104  }
105
106  private static IndexTypeDescriptor fromBackendIndexType(final BackendIndexCfgDefn.IndexType indexType)
107  {
108    switch (indexType)
109    {
110    case APPROXIMATE:
111      return APPROXIMATE;
112    case EQUALITY:
113      return EQUALITY;
114    case EXTENSIBLE:
115      return EXTENSIBLE;
116    case ORDERING:
117      return ORDERING;
118    case PRESENCE:
119      return PRESENCE;
120    case SUBSTRING:
121      return SUBSTRING;
122    default:
123      throw new IllegalArgumentException("No IndexTypeDescriptor corresponding to: " + indexType);
124    }
125  }
126
127  /**
128   * Convert the provided {@code Set<BackendIndexCfgDefn.IndexType>} to a
129   * {@code Set<IndexTypeDescriptor>}.
130   *
131   * @param indexTypes
132   *          A set of {@code Set<BackendIndexCfgDefn.IndexType>}
133   * @return A set of {@code Set<IndexTypeDescriptor>} corresponding to the
134   *         provided {@code Set<BackendIndexCfgDefn.IndexType>}
135   */
136  public static Set<IndexTypeDescriptor> fromBackendIndexTypes(final Set<BackendIndexCfgDefn.IndexType> indexTypes)
137  {
138    final Set<IndexTypeDescriptor> indexTypeDescriptors = new LinkedHashSet<>();
139    for (final BackendIndexCfgDefn.IndexType indexType : indexTypes)
140    {
141      indexTypeDescriptors.add(fromBackendIndexType(indexType));
142    }
143    return indexTypeDescriptors;
144  }
145
146  /**
147   * Convert the provided {@code Set<IndexTypeDescriptor>} to a
148   * {@code Set<BackendIndexCfgDefn.IndexType>}.
149   *
150   * @param indexTypeDescriptors
151   *          A set of {@code Set<IndexTypeDescriptor>}
152   * @return A set of {@code Set<BackendIndexCfgDefn.IndexType>} corresponding
153   *         to the provided {@code Set<IndexTypeDescriptor>}
154   */
155  public static Set<BackendIndexCfgDefn.IndexType> toBackendIndexTypes(
156      final Set<IndexTypeDescriptor> indexTypeDescriptors)
157  {
158    final Set<BackendIndexCfgDefn.IndexType> indexTypes = new LinkedHashSet<>();
159    for (final IndexTypeDescriptor indexTypeDescriptor : indexTypeDescriptors)
160    {
161      indexTypes.add(indexTypeDescriptor.toBackendIndexType());
162    }
163    return indexTypes;
164  }
165
166  /**
167   * Convert the provided {@code Set<IndexTypeDescriptor>} to a
168   * {@code Set<org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType>}.
169   *
170   * @param indexTypeDescriptors
171   *          A set of {@code Set<IndexTypeDescriptor>}
172   * @return A set of
173   *         {@code Set<org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType>}
174   *         corresponding to the provided {@code Set<IndexTypeDescriptor>}
175   */
176  public static Set<org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType> toNewConfigBackendIndexTypes(
177      final Set<IndexTypeDescriptor> indexTypeDescriptors)
178  {
179    Set<org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType> newConfigIndexTypes = new HashSet<>();
180    for (IndexTypeDescriptor indexType : indexTypeDescriptors)
181    {
182      newConfigIndexTypes.add(indexType.backendIndexType);
183    }
184    return newConfigIndexTypes;
185  }
186
187}