All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.knightsoftnet.validators.shared.data.AbstractCreateClass Maven / Gradle / Ivy

Go to download

The MT Bean Validators is a collection of JSR-303/JSR-349/JSR-380 bean validators. It's extracted from GWT Bean Validators and contains no dependencies to GWT.

There is a newer version: 2.3.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package de.knightsoftnet.validators.shared.data;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Read gwt constants from properties file, server and client side shared parts.
 *
 * @author Manfred Tremmel
 *
 */
public abstract class AbstractCreateClass { // NOPMD, can't include abstract static methods

  private AbstractCreateClass() {
    super();
  }

  protected static PhoneCountryConstantsImpl createPhoneCountryConstants(final Locale locale) {
    final Map phoneCountryNames =
        CreatePhoneCountryConstantsClass.readPhoneCountryNames(locale);
    final Map phoneCountryCodes =
        CreatePhoneCountryConstantsClass.readPhoneCountryCodes(locale);
    final Set countryCodeData =
        readPhoneCountryProperties(locale, phoneCountryNames, phoneCountryCodes);
    return new PhoneCountryConstantsImpl(countryCodeData,
        createMapFromPhoneCountry(locale, countryCodeData, phoneCountryNames, phoneCountryCodes));
  }

  protected static Set readPhoneCountryProperties(final Locale locale,
      final Map phoneCountryNames, final Map phoneCountryCodes) {
    return phoneCountryCodes.entrySet().stream().map(country -> {
      final PhoneCountryCodeData countryEntry =
          new PhoneCountryCodeData(country.getKey(), phoneCountryNames.get(country.getValue()));
      final Map phoneRegionCodes;
      if (StringUtils.equals(country.getKey(), "49")) {
        // to much data in German file, has to be splitted into two separate property files
        phoneRegionCodes = new HashMap<>();
        phoneRegionCodes.putAll(
            CreatePhoneCountryConstantsClass.readPhoneRegionCodes(country.getKey(), locale));
        phoneRegionCodes.putAll(
            CreatePhoneCountryConstantsClass.readPhoneRegionCodes(country.getKey() + "b", locale));
      } else {
        phoneRegionCodes =
            CreatePhoneCountryConstantsClass.readPhoneRegionCodes(country.getKey(), locale);
      }
      phoneRegionCodes.entrySet().stream().forEachOrdered(region -> {
        final PhoneAreaCodeData areaData = new PhoneAreaCodeData(region.getKey(), region.getValue(),
            StringUtils.length(country.getKey()));
        countryEntry.addAreaCodeData(areaData);
      });
      return countryEntry;
    }).collect(Collectors.toSet());
  }

  protected static Map createMapFromPhoneCountry(final Locale locale,
      final Set countries, final Map phoneCountryNames,
      final Map phoneCountryCodes) {
    final Map countryPhoneMap = new HashMap<>();
    final Map phoneTrunkAndExitCodes =
        CreatePhoneCountryConstantsClass.readPhoneTrunkAndExitCodes(locale);

    countries.stream().forEachOrdered(entry -> {
      final String countryCode = phoneCountryCodes.get(entry.getCountryCode());
      if (countryCode.contains("-")) {
        Arrays.asList(StringUtils.split(countryCode, '-')).stream().forEachOrdered(
            singleCountryCode -> entry.setPhoneCountryData(addCountryToPhoneMap(phoneCountryNames,
                countryPhoneMap, phoneTrunkAndExitCodes, entry, singleCountryCode))

        );
      } else {
        entry.setPhoneCountryData(addCountryToPhoneMap(phoneCountryNames, countryPhoneMap,
            phoneTrunkAndExitCodes, entry, countryCode));
      }
    });
    return countryPhoneMap;
  }

  protected static PhoneCountryData addCountryToPhoneMap(
      final Map phoneCountryNames,
      final Map countryPhoneMap,
      final Map phoneTrunkAndExitCodes, final PhoneCountryCodeData entry,
      final String countryCode) {
    final String trunkAndExitCodes = phoneTrunkAndExitCodes.get(countryCode);
    final String countryCodeName = phoneCountryNames.get(countryCode);
    final String[] splittedTrunkAndExitCodes =
        StringUtils.defaultString(trunkAndExitCodes).split(",");
    final String trunkCode =
        splittedTrunkAndExitCodes.length >= 1 ? splittedTrunkAndExitCodes[0] : StringUtils.EMPTY;
    final String exitCode =
        splittedTrunkAndExitCodes.length >= 2 ? splittedTrunkAndExitCodes[1] : StringUtils.EMPTY;
    final boolean areaCodeMustBeFilled =
        splittedTrunkAndExitCodes.length >= 3 && "1".equals(splittedTrunkAndExitCodes[2]);
    final PhoneCountryData countryData = new PhoneCountryData(countryCode, countryCodeName,
        trunkCode, exitCode, areaCodeMustBeFilled, entry);
    countryPhoneMap.put(countryCode, countryData);
    return countryData;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy