org.eclipse.xtext.xbase.lib.IntegerRange Java Examples

The following examples show how to use org.eclipse.xtext.xbase.lib.IntegerRange. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Solution_021.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public static void main(final String[] args) {
  HashSet<Integer> result = CollectionLiterals.<Integer>newHashSet();
  final int max = 10000;
  IntegerRange _upTo = new IntegerRange(1, max);
  for (final Integer i : _upTo) {
    boolean _contains = result.contains(i);
    boolean _not = (!_contains);
    if (_not) {
      final Integer sumOfDivisors = Solution_021.sumOfDivisors((i).intValue());
      if (((!Objects.equal(sumOfDivisors, i)) && ((sumOfDivisors).intValue() <= max))) {
        final Integer otherSumOfDivisors = Solution_021.sumOfDivisors((sumOfDivisors).intValue());
        boolean _equals = Objects.equal(otherSumOfDivisors, i);
        if (_equals) {
          result.add(i);
          result.add(sumOfDivisors);
        }
      }
    }
  }
  final Function2<Integer, Integer, Integer> _function = (Integer i1, Integer i2) -> {
    return Integer.valueOf(((i1).intValue() + (i2).intValue()));
  };
  InputOutput.<Integer>println(IterableExtensions.<Integer>reduce(result, _function));
}
 
Example #2
Source File: MarkdownParserTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
public void outline02_level3() throws Exception {
	File file = file("outline.txt");
	this.parser.setOutlineDepthRange(new IntegerRange(3, Integer.MAX_VALUE));
	this.parser.setAutoSectionNumbering(false);
	String value = this.parser.transform(file);
	assertEquals("# Title\n\n## Title 0\n\nthis is a fake text done for testing. this is a fake text "
			+ "done for testing. this is a fake text done for testing.\n\n\n"
			+ "> * [Title 3](#title-3)\n"
			+ "\n\n\nthis is a fake text done for testing.\n\n# Title 1\n\nthis is a fake "
			+ "text done for testing. this is a fake text done for testing. this is a\n\n## Title 2\n"
			+ "\nfake text done for testing. this is a fake text\n\n### Title 3\n\n## Title 4\n"
			+ "\n## Title 5\n\ndone for testing. this is a fake text done\nfor testing. this is a "
			+ "fake text done for testing. this is a fake text done for testing. this is a fake text "
			+ "done\n\n# Title 6\nfor testing. this is a fake text done for testing. this is a fake "
			+ "text done for testing.",
			value);
}
 
Example #3
Source File: MarkdownParserTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
public void outline02_level2() throws Exception {
	File file = file("outline.txt");
	this.parser.setOutlineDepthRange(new IntegerRange(2, Integer.MAX_VALUE));
	this.parser.setAutoSectionNumbering(false);
	String value = this.parser.transform(file);
	assertEquals("# Title\n\n## Title 0\n\nthis is a fake text done for testing. this is a fake text "
			+ "done for testing. this is a fake text done for testing.\n\n\n"
			+ "> * [Title 0](#title-0)\n"
			+ "> * [Title 2](#title-2)\n"
			+ "> \t* [Title 3](#title-3)\n"
			+ "> * [Title 4](#title-4)\n"
			+ "> * [Title 5](#title-5)\n"
			+ "\n\n\nthis is a fake text done for testing.\n\n# Title 1\n\nthis is a fake "
			+ "text done for testing. this is a fake text done for testing. this is a\n\n## Title 2\n"
			+ "\nfake text done for testing. this is a fake text\n\n### Title 3\n\n## Title 4\n"
			+ "\n## Title 5\n\ndone for testing. this is a fake text done\nfor testing. this is a "
			+ "fake text done for testing. this is a fake text done for testing. this is a fake text "
			+ "done\n\n# Title 6\nfor testing. this is a fake text done for testing. this is a fake "
			+ "text done for testing.",
			value);
}
 
Example #4
Source File: MarkdownParserTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
public void outline02_level1() throws Exception {
	File file = file("outline.txt");
	this.parser.setOutlineDepthRange(new IntegerRange(1, Integer.MAX_VALUE));
	this.parser.setAutoSectionNumbering(false);
	String value = this.parser.transform(file);
	assertEquals("# Title\n\n## Title 0\n\nthis is a fake text done for testing. this is a fake text "
			+ "done for testing. this is a fake text done for testing.\n\n\n"
			+ "> * [Title](#title)\n"
			+ "> \t* [Title 0](#title-0)\n"
			+ "> * [Title 1](#title-1)\n"
			+ "> \t* [Title 2](#title-2)\n"
			+ "> \t\t* [Title 3](#title-3)\n"
			+ "> \t* [Title 4](#title-4)\n"
			+ "> \t* [Title 5](#title-5)\n"
			+ "> * [Title 6](#title-6)\n"
			+ "\n\n\nthis is a fake text done for testing.\n\n# Title 1\n\nthis is a fake "
			+ "text done for testing. this is a fake text done for testing. this is a\n\n## Title 2\n"
			+ "\nfake text done for testing. this is a fake text\n\n### Title 3\n\n## Title 4\n"
			+ "\n## Title 5\n\ndone for testing. this is a fake text done\nfor testing. this is a "
			+ "fake text done for testing. this is a fake text done for testing. this is a fake text "
			+ "done\n\n# Title 6\nfor testing. this is a fake text done for testing. this is a fake "
			+ "text done for testing.",
			value);
}
 
Example #5
Source File: MarkdownParserTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
public void outline01_level3() throws Exception {
	File file = file("outline.txt");
	this.parser.setOutlineDepthRange(new IntegerRange(3, Integer.MAX_VALUE));
	this.parser.setAutoSectionNumbering(true);
	String value = this.parser.transform(file);
	assertEquals("# Title\n\n## Title 0\n\nthis is a fake text done for testing. this is a fake text done "
			+ "for testing. this is a fake text done for testing.\n\n\n"
			+ "> * [1. Title 3](#1-title-3)\n"
			+ "\n\n\nthis is a fake text done for testing.\n\n# Title 1\n\n"
			+ "this is a fake text done for testing. this is a fake text done for testing. this is a\n"
			+ "\n## Title 2\n\nfake text done for testing. this is a fake text\n\n### 1. Title 3\n\n"
			+ "## Title 4\n\n## Title 5\n\ndone for testing. this is a fake text done\nfor testing. "
			+ "this is a fake text done for testing. this is a fake text done for testing. this is a fake "
			+ "text done\n\n# Title 6\nfor testing. this is a fake text done for testing. this is a fake "
			+ "text done for testing.",
			value);
}
 
Example #6
Source File: MarkdownParserTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
public void outline01_level2() throws Exception {
	File file = file("outline.txt");
	this.parser.setOutlineDepthRange(new IntegerRange(2, Integer.MAX_VALUE));
	this.parser.setAutoSectionNumbering(true);
	String value = this.parser.transform(file);
	assertEquals("# Title\n\n## 1. Title 0\n\nthis is a fake text done for testing. this is a fake text done "
			+ "for testing. this is a fake text done for testing.\n\n\n"
			+ "> * [1. Title 0](#1-title-0)\n"
			+ "> * [2. Title 2](#2-title-2)\n"
			+ "> \t* [2.1. Title 3](#2-1-title-3)\n"
			+ "> * [3. Title 4](#3-title-4)\n"
			+ "> * [4. Title 5](#4-title-5)\n"
			+ "\n\n\nthis is a fake text done for testing.\n\n# Title 1\n\n"
			+ "this is a fake text done for testing. this is a fake text done for testing. this is a\n"
			+ "\n## 2. Title 2\n\nfake text done for testing. this is a fake text\n\n### 2.1. Title 3\n\n"
			+ "## 3. Title 4\n\n## 4. Title 5\n\ndone for testing. this is a fake text done\nfor testing. "
			+ "this is a fake text done for testing. this is a fake text done for testing. this is a fake "
			+ "text done\n\n# Title 6\nfor testing. this is a fake text done for testing. this is a fake "
			+ "text done for testing.",
			value);
}
 
Example #7
Source File: MarkdownParserTest.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Test
public void outline01_level1() throws Exception {
	File file = file("outline.txt");
	this.parser.setOutlineDepthRange(new IntegerRange(1, Integer.MAX_VALUE));
	this.parser.setAutoSectionNumbering(true);
	String value = this.parser.transform(file);
	assertEquals("# 1. Title\n\n## 1.1. Title 0\n\nthis is a fake text done for testing. this is a fake text done "
			+ "for testing. this is a fake text done for testing.\n\n\n"
			+ "> * [1. Title](#1-title)\n"
			+ "> \t* [1.1. Title 0](#1-1-title-0)\n"
			+ "> * [2. Title 1](#2-title-1)\n"
			+ "> \t* [2.1. Title 2](#2-1-title-2)\n"
			+ "> \t\t* [2.1.1. Title 3](#2-1-1-title-3)\n"
			+ "> \t* [2.2. Title 4](#2-2-title-4)\n"
			+ "> \t* [2.3. Title 5](#2-3-title-5)\n"
			+ "> * [3. Title 6](#3-title-6)\n"
			+ "\n\n\nthis is a fake text done for testing.\n\n# 2. Title 1\n\n"
			+ "this is a fake text done for testing. this is a fake text done for testing. this is a\n"
			+ "\n## 2.1. Title 2\n\nfake text done for testing. this is a fake text\n\n### 2.1.1. Title 3\n\n"
			+ "## 2.2. Title 4\n\n## 2.3. Title 5\n\ndone for testing. this is a fake text done\nfor testing. "
			+ "this is a fake text done for testing. this is a fake text done for testing. this is a fake "
			+ "text done\n\n# 3. Title 6\n\nfor testing. this is a fake text done for testing. this is a fake "
			+ "text done for testing.",
			value);
}
 
Example #8
Source File: MarkdownParser.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
public String extractPageTitle(String content) {
	final Pattern sectionPattern = Pattern.compile(
			isAutoSectionNumbering() ? SECTION_PATTERN_AUTONUMBERING : SECTION_PATTERN_NO_AUTONUMBERING,
					Pattern.MULTILINE);
	final Matcher matcher = sectionPattern.matcher(content);
	final IntegerRange depthRange = getOutlineDepthRange();
	final int titleGroupId;
	if (isAutoSectionNumbering()) {
		titleGroupId = 3;
	} else {
		titleGroupId = 2;
	}
	while (matcher.find()) {
		final String prefix = matcher.group(1);
		final int clevel = prefix.length();
		if (clevel < depthRange.getStart()) {
			final String title = matcher.group(titleGroupId);
			if (!Strings.isEmpty(title)) {
				return title;
			}
		}
	}
	return null;
}
 
Example #9
Source File: DefaultTaskParserTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testLongInputManyTasks() {
  final int expectation = 100000;
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("/*");
  _builder.newLine();
  {
    IntegerRange _upTo = new IntegerRange(1, expectation);
    for(final Integer i : _upTo) {
      _builder.append(" ");
      _builder.append("* FIXME this cannot work");
      _builder.newLine();
    }
  }
  _builder.append(" ");
  _builder.append("*/");
  _builder.newLine();
  final String source = _builder.toString();
  final List<Task> parsed = this.parser.parseTasks(LineDelimiters.toUnix(source), this.definitions);
  Assert.assertEquals(expectation, parsed.size());
  ExclusiveRange _doubleDotLessThan = new ExclusiveRange(0, expectation, true);
  for (final Integer i_1 : _doubleDotLessThan) {
    Assert.assertEquals(((i_1).intValue() + 2), parsed.get((i_1).intValue()).getLineNumber());
  }
}
 
Example #10
Source File: XtendIncrementalBuilderPerformanceTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public void doTestPerformance(final int max) {
  final Procedure1<BuildRequest> _function = (BuildRequest it) -> {
    final Function1<Integer, URI> _function_1 = (Integer it_1) -> {
      return this.toFile((it_1).intValue(), max);
    };
    it.setDirtyFiles(IterableExtensions.<URI>toList(IterableExtensions.<Integer, URI>map(new IntegerRange(1, max), _function_1)));
  };
  final BuildRequest buildRequest = this.newBuildRequest(_function);
  final Stopwatch sw = Stopwatch.createStarted();
  this.build(buildRequest);
  StringConcatenation _builder = new StringConcatenation();
  _builder.append(max);
  _builder.append(" file took ");
  long _elapsed = sw.elapsed(TimeUnit.MILLISECONDS);
  _builder.append(_elapsed);
  _builder.append(" ms");
  InputOutput.<String>println(_builder.toString());
}
 
Example #11
Source File: Solution_021.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public static Integer sumOfDivisors(final int input) {
  Integer _xblockexpression = null;
  {
    final int sqrt = Double.valueOf(Math.floor(Math.sqrt(input))).intValue();
    final Function1<Integer, Boolean> _function = (Integer div) -> {
      return Boolean.valueOf(((input % (div).intValue()) == 0));
    };
    final Function2<Integer, Integer, Integer> _function_1 = (Integer i1, Integer i2) -> {
      int _xblockexpression_1 = (int) 0;
      {
        final int other = (input / (i2).intValue());
        int _xifexpression = (int) 0;
        if ((other != (i2).intValue())) {
          _xifexpression = (((i1).intValue() + (i2).intValue()) + other);
        } else {
          _xifexpression = ((i1).intValue() + (i2).intValue());
        }
        _xblockexpression_1 = _xifexpression;
      }
      return Integer.valueOf(_xblockexpression_1);
    };
    _xblockexpression = IterableExtensions.<Integer, Integer>fold(IterableExtensions.<Integer>filter(new IntegerRange(2, sqrt), _function), Integer.valueOf(1), _function_1);
  }
  return _xblockexpression;
}
 
Example #12
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public void generateParameters(final JvmExecutable it, final ITreeAppendable appendable, final GeneratorConfig config) {
  boolean _isEmpty = it.getParameters().isEmpty();
  boolean _not = (!_isEmpty);
  if (_not) {
    int _size = it.getParameters().size();
    int _minus = (_size - 1);
    IntegerRange _upTo = new IntegerRange(0, _minus);
    for (final Integer i : _upTo) {
      {
        int _size_1 = it.getParameters().size();
        final boolean last = (((i).intValue() + 1) == _size_1);
        final JvmFormalParameter p = it.getParameters().get((i).intValue());
        this.generateParameter(p, appendable, (last && it.isVarArgs()), config);
        if ((!last)) {
          appendable.append(", ");
        }
      }
    }
  }
}
 
Example #13
Source File: Solution_005.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public static void main(final String[] args) {
  ArrayList<Integer> result = CollectionLiterals.<Integer>newArrayList();
  IntegerRange _upTo = new IntegerRange(2, 20);
  for (final Integer i : _upTo) {
    {
      Integer j = i;
      for (final Integer k : result) {
        if ((((j).intValue() % (k).intValue()) == 0)) {
          j = Integer.valueOf(((j).intValue() / (k).intValue()));
        }
      }
      if (((j).intValue() != 1)) {
        result.add(j);
      }
    }
  }
  final Function2<Integer, Integer, Integer> _function = (Integer m1, Integer m2) -> {
    return Integer.valueOf(((m1).intValue() * (m2).intValue()));
  };
  InputOutput.<Integer>println(IterableExtensions.<Integer>reduce(result, _function));
}
 
Example #14
Source File: Solution_004.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public static void main(final String[] args) {
  int result = 0;
  IntegerRange _upTo = new IntegerRange(100, 999);
  for (final Integer left : _upTo) {
    IntegerRange _upTo_1 = new IntegerRange((left).intValue(), 999);
    for (final Integer right : _upTo_1) {
      {
        int candidate = ((left).intValue() * (right).intValue());
        if ((candidate > result)) {
          List<Character> charList = (List<Character>)Conversions.doWrapArray(Integer.valueOf(candidate).toString().toCharArray());
          List<Character> _reverseView = ListExtensions.<Character>reverseView(charList);
          boolean _equals = Objects.equal(_reverseView, charList);
          if (_equals) {
            result = candidate;
          }
        }
      }
    }
  }
  InputOutput.<Integer>println(Integer.valueOf(result));
}
 
Example #15
Source File: Solution_023.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public static boolean isAbundant(final int input) {
  final int sqrt = Double.valueOf(Math.floor(Math.sqrt(input))).intValue();
  final Function1<Integer, Boolean> _function = (Integer div) -> {
    return Boolean.valueOf((((div).intValue() != input) && ((input % (div).intValue()) == 0)));
  };
  final Function2<Integer, Integer, Integer> _function_1 = (Integer i1, Integer i2) -> {
    int _xblockexpression = (int) 0;
    {
      final int other = (input / (i2).intValue());
      int _xifexpression = (int) 0;
      if (((other != (i2).intValue()) && (other != input))) {
        _xifexpression = (((i1).intValue() + (i2).intValue()) + other);
      } else {
        _xifexpression = ((i1).intValue() + (i2).intValue());
      }
      _xblockexpression = _xifexpression;
    }
    return Integer.valueOf(_xblockexpression);
  };
  final Integer sumOfDivisors = IterableExtensions.<Integer, Integer>fold(IterableExtensions.<Integer>filter(new IntegerRange(2, sqrt), _function), Integer.valueOf(1), _function_1);
  return ((sumOfDivisors).intValue() > input);
}
 
Example #16
Source File: FormattableDocument.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public String getIndentation(final int levels) {
  String _xifexpression = null;
  if ((levels > 0)) {
    String _xblockexpression = null;
    {
      final String indent = this.cfg.get(BasicFormatterPreferenceKeys.indentation);
      final Function1<Integer, String> _function = (Integer it) -> {
        return indent;
      };
      _xblockexpression = IterableExtensions.join(IterableExtensions.<Integer, String>map(new IntegerRange(0, (levels - 1)), _function));
    }
    _xifexpression = _xblockexpression;
  } else {
    _xifexpression = "";
  }
  return _xifexpression;
}
 
Example #17
Source File: FormattableDocument.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public String getWrap(final int levels) {
  String _xifexpression = null;
  if ((levels > 0)) {
    String _xblockexpression = null;
    {
      final String sep = this.cfg.get(BasicFormatterPreferenceKeys.lineSeparator);
      final Function1<Integer, String> _function = (Integer it) -> {
        return sep;
      };
      _xblockexpression = IterableExtensions.join(IterableExtensions.<Integer, String>map(new IntegerRange(0, (levels - 1)), _function));
    }
    _xifexpression = _xblockexpression;
  } else {
    _xifexpression = "";
  }
  return _xifexpression;
}
 
Example #18
Source File: IntegerRangeTest.java    From xtext-lib with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testValidity() {
	new IntegerRange(0, 0, 1);
	new IntegerRange(0, 0, -1);
	new IntegerRange(0, 2, 1);
	new IntegerRange(-1, 2, 1);
	new IntegerRange(-2, -1, 1);
	new IntegerRange(2, 0, -1);
	new IntegerRange(2, -1, -1);
	new IntegerRange(-1, -2, -1);
	new IntegerRange(-2, 0, 1);
	new IntegerRange(Integer.MIN_VALUE, 0, 1);
	new IntegerRange(Integer.MAX_VALUE, 0, -1);

	assertInvalid(0, 1, 0);
	assertInvalid(0, 2, -1);
	assertInvalid(-1, 2, -1);
	assertInvalid(-2, -1, -1);
	assertInvalid(2, 0, 1);
	assertInvalid(2, -1, 1);
	assertInvalid(-1, -2, 1);
}
 
Example #19
Source File: ActualTypeArgumentMergeTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public Map<JvmTypeParameter, List<LightweightBoundTypeArgument>> mappedBy(final String typeParameters, final String... alternatingTypeReferences) {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("def ");
    {
      boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(typeParameters);
      boolean _not = (!_isNullOrEmpty);
      if (_not) {
        _builder.append("<");
        _builder.append(typeParameters);
        _builder.append(">");
      }
    }
    _builder.append(" void method(");
    final Function1<String, CharSequence> _function = (String it) -> {
      return it;
    };
    String _join = IterableExtensions.<String>join(((Iterable<String>)Conversions.doWrapArray(alternatingTypeReferences)), null, " p, ", " p", _function);
    _builder.append(_join);
    _builder.append(") {}");
    final String signature = _builder.toString();
    final XtendFunction function = this.function(signature.toString());
    final JvmOperation operation = this._iXtendJvmAssociations.getDirectlyInferredOperation(function);
    EList<JvmTypeParameter> _typeParameters = operation.getTypeParameters();
    ITypeReferenceOwner _owner = this.getOwner();
    final ActualTypeArgumentCollector collector = new ActualTypeArgumentCollector(_typeParameters, BoundTypeArgumentSource.INFERRED, _owner);
    int _size = ((List<String>)Conversions.doWrapArray(alternatingTypeReferences)).size();
    int _minus = (_size - 1);
    IntegerRange _withStep = new IntegerRange(0, _minus).withStep(2);
    for (final Integer i : _withStep) {
      collector.populateTypeParameterMapping(this.toLightweightTypeReference(operation.getParameters().get((i).intValue()).getParameterType()), this.toLightweightTypeReference(operation.getParameters().get(((i).intValue() + 1)).getParameterType()));
    }
    return collector.getTypeParameterMapping();
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #20
Source File: FormattableDocument.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isWhitespace(final String doc) {
  int _length = doc.length();
  int _minus = (_length - 1);
  final Function1<Integer, Boolean> _function = (Integer it) -> {
    return Boolean.valueOf(Character.isWhitespace(doc.charAt((it).intValue())));
  };
  return IterableExtensions.<Integer>forall(new IntegerRange(0, _minus), _function);
}
 
Example #21
Source File: AbstractTypeResolverTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@IgnoredBySmokeTest("Do not run smoke test with 1000s of nested expressions")
@Test
public void testBlockExpression_03() throws Exception {
  String input = "{ val s1 = \'\'\n";
  final int max = 1000;
  IntegerRange _upTo = new IntegerRange(1, max);
  for (final Integer i : _upTo) {
    input = (((((input + " val s") + Integer.valueOf(((i).intValue() + 1))) + " = s") + i) + "\n");
  }
  input = (((input + " s") + Integer.valueOf((max + 1))) + "}");
  this.resolvesTo(input, "String");
}
 
Example #22
Source File: IntegerRangeTest.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertInvalid(int start, int end, int step) {
	try {
		new IntegerRange(start, end, step);
		fail("Invalid step not detected");
	} catch (IllegalArgumentException e) {
		// expected exception
	}
}
 
Example #23
Source File: AbstractMarkerLanguageParser.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Parse a range of integers.
 *
 * <p>The supported formats are:<ul>
 * <li>{@code n} for exactly {@code n},</li>
 * <li>{@code n-m} for {@code n} to {@code m},</li>
 * <li>{@code n-} for {@code n} to infinity,</li>
 * <li>{@code -m} for 1 to {@code n}.</li>
 * </ul>
 *
 * @param stringRange the string representation to parse.
 * @param minValue the minimal value in the possible values for the range.
 * @return the integer range.
 */
public static IntegerRange parseRange(String stringRange, int minValue) {
	final String sepPattern = "[,;\\-:]"; //$NON-NLS-1$
	try {
		final Matcher matcher = Pattern.compile("^\\s*" //$NON-NLS-1$
				+ "(?:(?<left>[0-9]+)\\s*(?:(?<sep1>" + sepPattern + ")\\s*(?<right1>[0-9]+)?)?)" //$NON-NLS-1$ //$NON-NLS-2$
				+ "|(?:(?<sep2>" + sepPattern + ")\\s*(?<right2>[0-9]+))" //$NON-NLS-1$ //$NON-NLS-2$
				+ "\\s*$") //$NON-NLS-1$
				.matcher(stringRange);
		if (matcher.matches()) {
			final String left = matcher.group("left"); //$NON-NLS-1$
			final String sep = select(matcher.group("sep1"), matcher.group("sep2")); //$NON-NLS-1$ //$NON-NLS-2$
			final String right = select(matcher.group("right1"), matcher.group("right2")); //$NON-NLS-1$ //$NON-NLS-2$
			if (Strings.isEmpty(left)) {
				if (!Strings.isEmpty(sep) && !Strings.isEmpty(right)) {
					return new IntegerRange(minValue, Math.max(minValue, Integer.valueOf(right)));
				}
			} else {
				final int leftValue = Math.max(minValue, Integer.valueOf(left));
				if (Strings.isEmpty(sep)) {
					return new IntegerRange(leftValue, leftValue);
				}
				if (Strings.isEmpty(right)) {
					return new IntegerRange(leftValue, Integer.MAX_VALUE);
				}
				final int rightValue = Math.max(minValue, Integer.valueOf(right));
				if (rightValue < leftValue) {
					return new IntegerRange(rightValue, leftValue);
				}
				return new IntegerRange(leftValue, rightValue);
			}
		}
	} catch (Throwable exception) {
		throw new IllegalArgumentException(MessageFormat.format(Messages.GenerateMojo_4, stringRange), exception);
	}
	throw new IllegalArgumentException(MessageFormat.format(Messages.GenerateMojo_4, stringRange));
}
 
Example #24
Source File: MarkdownParser.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Change the level at which the titles may appear in the outline.
 *
 * @param level the level, at least 1.
 */
public void setOutlineDepthRange(IntegerRange level) {
	if (level == null) {
		this.outlineDepthRange = new IntegerRange(DEFAULT_OUTLINE_TOP_LEVEL, DEFAULT_OUTLINE_TOP_LEVEL);
	} else {
		this.outlineDepthRange = level;
	}
}
 
Example #25
Source File: IntegerRangeTest.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testContains_0() {
	IntegerRange range = new IntegerRange(-1, 1, 2);
	assertFalse(range.contains(-2));
	assertTrue(range.contains(-1));
	assertFalse(range.contains(0));
	assertTrue(range.contains(1));
	assertFalse(range.contains(2));
}
 
Example #26
Source File: TemplateNodeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testWeirdTemplateString() {
  StringConcatenationClient _client = new StringConcatenationClient() {
    @Override
    protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
      _builder.append("fooo bar");
      _builder.newLine();
      _builder.append("d   ");
      {
        if ((2 > 1)) {
          _builder.append("s");
          _builder.newLineIfNotEmpty();
          _builder.append("\t  ");
          _builder.append("ee  ");
          {
            IntegerRange _upTo = new IntegerRange(1, 4);
            for(final Integer i : _upTo) {
              _builder.newLineIfNotEmpty();
              _builder.newLineIfNotEmpty();
              CharSequence _other = TemplateNodeTest.this.other();
              _builder.append(_other);
              _builder.append(" ");
              _builder.append(i);
              _builder.newLineIfNotEmpty();
              String _multiLineString = TemplateNodeTest.this.multiLineString();
              _builder.append(_multiLineString);
              _builder.append(" ");
              String _multiLineString_1 = TemplateNodeTest.this.multiLineString();
              _builder.append(_multiLineString_1);
              _builder.newLineIfNotEmpty();
            }
          }
        }
      }
    }
  };
  this.assertEquals(_client);
}
 
Example #27
Source File: RichStringToLineModel.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void acceptSemanticText(final CharSequence text, final RichStringLiteral origin) {
  super.acceptSemanticText(text, origin);
  if ((!this.content)) {
    if (((text.length() > 0) && (IterableExtensions.<Integer, Boolean>fold(new IntegerRange(0, (text.length() - 1)), Boolean.valueOf(false), ((Function2<Boolean, Integer, Boolean>) (Boolean v, Integer i) -> {
      return Boolean.valueOf(((v).booleanValue() || (!Character.isWhitespace(text.charAt((i).intValue())))));
    }))).booleanValue())) {
      this.startContent();
    }
  }
  int _length = text.length();
  int _plus = (this.offset + _length);
  this.offset = _plus;
}
 
Example #28
Source File: IntegerRangeTest.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testContains_1() {
	IntegerRange range = new IntegerRange(-1, 2, 2);
	assertFalse(range.contains(-2));
	assertTrue(range.contains(-1));
	assertFalse(range.contains(0));
	assertTrue(range.contains(1));
	assertFalse(range.contains(2));
}
 
Example #29
Source File: Solution_003.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public boolean isPrime(final long l) {
  int _intValue = Double.valueOf(Math.sqrt(l)).intValue();
  final Function1<Integer, Boolean> _function = (Integer i) -> {
    return Boolean.valueOf(((l % (i).intValue()) == 0));
  };
  return IterableExtensions.isEmpty(IterableExtensions.<Integer>filter(new IntegerRange(2, _intValue), _function));
}
 
Example #30
Source File: ActualTypeArgumentCollectorTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public Map<JvmTypeParameter, List<LightweightBoundTypeArgument>> mappedBy(final String typeParameters, final String... alternatingTypeReferences) {
  final JvmOperation operation = this.operation(typeParameters, alternatingTypeReferences);
  EList<JvmTypeParameter> _typeParameters = operation.getTypeParameters();
  ITypeReferenceOwner _owner = this.getOwner();
  final ActualTypeArgumentCollector collector = new ActualTypeArgumentCollector(_typeParameters, BoundTypeArgumentSource.INFERRED, _owner);
  int _size = ((List<String>)Conversions.doWrapArray(alternatingTypeReferences)).size();
  int _minus = (_size - 1);
  IntegerRange _withStep = new IntegerRange(0, _minus).withStep(2);
  for (final Integer i : _withStep) {
    collector.populateTypeParameterMapping(this.toLightweightTypeReference(operation.getParameters().get((i).intValue()).getParameterType()), this.toLightweightTypeReference(operation.getParameters().get(((i).intValue() + 1)).getParameterType()));
  }
  return collector.getTypeParameterMapping();
}