001/*
002 * JPPF.
003 * Copyright (C) 2005-2016 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * 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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.net;
020
021import java.util.regex.Pattern;
022
023import org.jppf.utils.Range;
024
025/**
026 * Instances of this class represent the configuration parameters for a specific IP address pattern implementation.
027 * @author Laurent Cohen
028 */
029public final class PatternConfiguration
030{
031  /**
032   * Configuration for IPv4 address patterns.
033   */
034  public static final PatternConfiguration IPV4_CONFIGURATION = new PatternConfiguration(4, 0, 255, '.', "");
035  /**
036   * Configuration for IPv6 address patterns.
037   */
038  public static final PatternConfiguration IPV6_CONFIGURATION = new PatternConfiguration(8, 0, 0xffff, ':', "0x");
039  /**
040   * The separator for the components of an address.
041   */
042  private final char compSeparator;
043  /**
044   * Regex pattern that matches any one dot.
045   */
046  final Pattern compSeparatorPattern;
047  /**
048   * Constant representing the [minValue,maxValue] range.
049   */
050  final Range<Integer> fullRange;
051  /**
052   * Number of components in the address.
053   */
054  final int nbComponents;
055  /**
056   * Minimum value of a component.
057   */
058  final int minValue;
059  /**
060   * Maximum value of a component.
061   */
062  final int maxValue;
063  /**
064   * The prefix indicating in which base the numbers are represented, i.e. "" for decimal, "0x" for hexadecimal.
065   */
066  final String valuePrefix;
067
068  /**
069   * Initialize this pattern configuration with the specified values.
070   * @param nbComponents the number of components in the address.
071   * @param minValue the minimum value of a component.
072   * @param maxValue the maximum value of a component.
073   * @param compSeparator the separator for the components of an address.
074   * @param valuePrefix the prefix indicating in which base the numbers are represented, i.e. "" for decimal, "0X" for hexadecimal.
075   */
076  public PatternConfiguration(final int nbComponents, final int minValue, final int maxValue, final char compSeparator, final String valuePrefix)
077  {
078    this.nbComponents = nbComponents;
079    this.minValue = minValue;
080    this.maxValue = maxValue;
081    this.compSeparator = compSeparator;
082    fullRange = new Range<>(minValue, maxValue);
083    compSeparatorPattern = Pattern.compile((compSeparator == '.' ? "\\" : "") + compSeparator);
084    this.valuePrefix = valuePrefix;
085  }
086
087  /**
088   * Get the separator for the components of an address.
089   * @return the separator character.
090   */
091  protected char getCompSeparator()
092  {
093    return compSeparator;
094  }
095}