/**
   * Adds a dialect to the <code>initialVariantMap</code>.
   *
   * @param dialectSpec the <code>ConceptSpec</code> representing the dialect concept
   * @param viewCoordinate the view coordinate specifying which versions are active or inactive
   * @param varientsSpec the <code>ConceptSpec</code> representing the dialect refex concept
   * @param terminologySnapshot the terminologySnapshot to use for getting component versions
   * @param initialVariantMap the map to udpate
   * @throws ContradictionException if more than one version is found for a given position or view
   *     coordinate
   * @throws IOException signals that an I/O exception has occurred
   */
  private static void addDialect(
      ConceptSpec dialectSpec,
      ViewCoordinate viewCoordinate,
      ConceptSpec varientsSpec,
      TerminologySnapshotDI terminologySnapshot,
      HashMap<Integer, Map<String, String>> initialVariantMap)
      throws ContradictionException, IOException {
    ConceptVersionBI dialectC = dialectSpec.getStrict(viewCoordinate);
    ConceptVersionBI variantTextRefsetC = varientsSpec.getStrict(viewCoordinate);

    Collection<? extends RefexChronicleBI<?>> dialectVarients =
        variantTextRefsetC.getRefexMembersActive(viewCoordinate);
    Map<String, String> variantDialectMap = new HashMap<String, String>();
    for (RefexChronicleBI<?> refex : dialectVarients) {
      if (RefexStringVersionBI.class.isAssignableFrom(refex.getClass())) {
        RefexStringVersionBI<?> dialectText =
            (RefexStringVersionBI<?>) refex.getVersion(viewCoordinate);
        if (dialectText != null) {
          RefexStringVersionBI<?> variantText =
              (RefexStringVersionBI<?>)
                  terminologySnapshot.getComponentVersion(dialectText.getReferencedComponentNid());
          variantDialectMap.put(variantText.getString1(), dialectText.getString1());
        }
      }
    }
    initialVariantMap.put(dialectC.getNid(), variantDialectMap);
  }
  /**
   * Imports the dialect variant words into a map of the dialect to the dialect variant word. Checks
   * to see if the map is empty before loading, and will only load the words if the map is found to
   * be empty.
   *
   * @param dialectOrLanguageNid the dialect or language nid
   * @throws UnsupportedDialectOrLanguage indicates an unsupported dialect or language
   * @throws IOException signals that an I/O exception has occurred
   */
  private static void lazyInit(int dialectOrLanguageNid)
      throws UnsupportedDialectOrLanguage, IOException {
    if (variantMap == null) {
      initLock.lock();
      try {
        if (variantMap == null) {
          HashMap<Integer, Map<String, String>> initialVariantMap =
              new HashMap<Integer, Map<String, String>>();
          variantSetMap = new HashMap<Integer, Set<String>>();
          ViewCoordinate vc = AppContext.getService(TerminologyStoreDI.class).getMetadataVC();
          TerminologySnapshotDI ts =
              AppContext.getService(TerminologyStoreDI.class).getSnapshot(vc);

          ConceptVersionBI enVariantTextRefsetC =
              Language.EN_VARIANT_TEXT.getStrict(
                  AppContext.getService(TerminologyStoreDI.class).getMetadataVC());
          Collection<? extends RefexChronicleBI<?>> enVariants = enVariantTextRefsetC.getRefexes();
          Set<String> variantSet = new HashSet<String>();
          for (RefexChronicleBI<?> refex : enVariants) {
            if (RefexStringVersionBI.class.isAssignableFrom(refex.getClass())) {
              RefexStringVersionBI<?> variantText = (RefexStringVersionBI<?>) refex.getVersion(vc);
              if (variantText != null) {
                variantSet.add(variantText.getString1());
              }
            }
          }
          variantSetMap.put(Language.EN.getStrict(vc).getNid(), variantSet);

          addDialect(Language.EN_AU, vc, Language.EN_AU_TEXT_VARIANTS, ts, initialVariantMap);
          addDialect(Language.EN_CA, vc, Language.EN_CA_TEXT_VARIANTS, ts, initialVariantMap);
          addDialect(Language.EN_NZ, vc, Language.EN_NZ_TEXT_VARIANTS, ts, initialVariantMap);
          addDialect(Language.EN_UK, vc, Language.EN_UK_TEXT_VARIANTS, ts, initialVariantMap);
          addDialect(Language.EN_US, vc, Language.EN_US_TEXT_VARIANTS, ts, initialVariantMap);
          DialectHelper.variantMap = initialVariantMap;
        }
      } catch (ContradictionException ex) {
        throw new IOException(ex);
      } finally {
        initLock.unlock();
      }
    }
    if (!variantMap.containsKey(dialectOrLanguageNid)
        && !variantSetMap.containsKey(dialectOrLanguageNid)) {
      throw new UnsupportedDialectOrLanguage("nid: " + dialectOrLanguageNid);
    }
  }