下面列出了怎么用org.eclipse.xtext.xbase.lib.IntegerRange的API类实例代码及写法,或者点击链接到github查看源代码。
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(", ");
}
}
}
}
}
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;
}
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;
}
@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);
}
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);
}
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));
}
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));
}
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));
}
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;
}
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());
}
@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());
}
}
@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;
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
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);
}
@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");
}
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
}
}
@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));
}
@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));
}
@Test
public void testSize() {
final IntegerRange myRange = new IntegerRange(-1, 1);
assertEquals(3, myRange.getSize());
assertEquals(2, myRange.withStep(2).getSize());
assertEquals(1, myRange.withStep(3).getSize());
final IntegerRange myRange2 = new IntegerRange(1, -1);
assertEquals(3, myRange2.getSize());
assertEquals(2, myRange2.withStep(-2).getSize());
assertEquals(1, myRange2.withStep(-3).getSize());
}
public void testPerformance() {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("import org.eclipse.xtend.core.tests.restricted.RestrictedClass");
_builder.newLine();
_builder.append("import org.eclipse.xtend.core.tests.internal.InternalClass");
_builder.newLine();
_builder.newLine();
_builder.append("class Foo {");
_builder.newLine();
{
IntegerRange _upTo = new IntegerRange(0, 200);
for(final Integer i : _upTo) {
_builder.append("RestrictedClass x");
_builder.append(i);
_builder.append(" = new RestrictedClass");
_builder.newLineIfNotEmpty();
_builder.append("InternalClass y");
_builder.append(i);
_builder.append(" = new InternalClass");
_builder.newLineIfNotEmpty();
_builder.append("def bar(InternalClass p1");
_builder.append(i);
_builder.append(", RestrictedClass p2");
_builder.append(i);
_builder.append("){}");
_builder.newLineIfNotEmpty();
}
}
_builder.append("}");
_builder.newLine();
final XtendFile xtendFile = this.testHelper.xtendFile("Clazz.xtend", _builder.toString());
this.helper.validate(xtendFile);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
public CharSequence singTheSong(final int all) {
StringConcatenation _builder = new StringConcatenation();
{
IntegerRange _upTo = new IntegerRange(all, 1);
for(final Integer i : _upTo) {
String _Bottles = BottleSupport.Bottles((i).intValue());
_builder.append(_Bottles);
_builder.append(" of beer on the wall, ");
String _bottles = BottleSupport.bottles((i).intValue());
_builder.append(_bottles);
_builder.append(" of beer.");
_builder.newLineIfNotEmpty();
_builder.append("Take one down and pass it around, ");
String _bottles_1 = BottleSupport.bottles(((i).intValue() - 1));
_builder.append(_bottles_1);
_builder.append(" of beer on the wall.");
_builder.newLineIfNotEmpty();
_builder.newLine();
}
}
_builder.append("No more bottles of beer on the wall, no more bottles of beer.");
_builder.newLine();
_builder.append("Go to the store and buy some more, ");
String _bottles_2 = BottleSupport.bottles(all);
_builder.append(_bottles_2);
_builder.append(" of beer on the wall.");
_builder.newLineIfNotEmpty();
return _builder;
}
public static void main(final String[] args) {
final Function2<Integer, Integer, Integer> _function = (Integer i1, Integer i2) -> {
return Integer.valueOf(((i1).intValue() + (i2).intValue()));
};
Integer result = IterableExtensions.<Integer>reduce(new IntegerRange(1, 100), _function);
result = Integer.valueOf(((result).intValue() * (result).intValue()));
final Function2<Integer, Integer, Integer> _function_1 = (Integer r, Integer i) -> {
return Integer.valueOf(((r).intValue() - ((i).intValue() * (i).intValue())));
};
result = IterableExtensions.<Integer, Integer>fold(new IntegerRange(1, 100), result, _function_1);
InputOutput.<Integer>println(result);
}
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));
}
public static void main(final String[] args) {
int n = 10001;
ArrayList<Integer> seenPrimes = CollectionLiterals.<Integer>newArrayList();
int slice = 0;
while (true) {
{
List<Integer> numbers = IterableExtensions.<Integer>toList(new IntegerRange(((slice * n) + 1), ((slice + 1) * n)));
for (final Integer prime : seenPrimes) {
Solution_007.markAsNotPrime((prime).intValue(), numbers);
}
slice = (slice + 1);
while ((!numbers.isEmpty())) {
{
Integer nextPrime = numbers.remove(0);
if (((nextPrime).intValue() != 1)) {
seenPrimes.add(nextPrime);
int _size = seenPrimes.size();
boolean _equals = (_size == n);
if (_equals) {
InputOutput.<Integer>println(nextPrime);
return;
}
Solution_007.markAsNotPrime((nextPrime).intValue(), numbers);
}
}
}
}
}
}
public static void main(final String[] args) {
final Function1<Integer, Boolean> _function = (Integer i) -> {
return Boolean.valueOf(((((i).intValue() % 3) == 0) || (((i).intValue() % 5) == 0)));
};
final Function2<Integer, Integer, Integer> _function_1 = (Integer i1, Integer i2) -> {
return Integer.valueOf(((i1).intValue() + (i2).intValue()));
};
InputOutput.<Integer>println(IterableExtensions.<Integer>reduce(IterableExtensions.<Integer>filter(new IntegerRange(1, 999), _function), _function_1));
}