Java Code Examples for org.eclipse.xtext.xbase.lib.IterableExtensions#forEach()

The following examples show how to use org.eclipse.xtext.xbase.lib.IterableExtensions#forEach() . 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: BatchIdentifiableTypeTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void resolvesIdentifiablesTo(String expression, String... types) {
	try {
		String expressionWithQualifiedNames = expression.replace("$$", "org::eclipse::xtext::xbase::lib::");
		XExpression xExpression = expression(expressionWithQualifiedNames, false);
		IResolvedTypes resolvedTypes = getTypeResolver().resolveTypes(xExpression);
		List<JvmIdentifiableElement> identifiables = findIdentifiables(xExpression);
		Assert.assertFalse(identifiables.isEmpty());
		Assert.assertEquals(types.length, identifiables.size());
		IterableExtensions.forEach(identifiables, (JvmIdentifiableElement identifiable, Integer index) -> {
			LightweightTypeReference type = resolvedTypes.getActualType(identifiable);
			Assert.assertNotNull(type);
			Assert.assertEquals("failed for identifiable at " + index, types[index], type.getSimpleName());
		});
	} catch (Exception e) {
		throw new AssertionError(e);
	}
}
 
Example 2
Source File: XContentAssistService.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Create the completion proposals.
 */
public CompletionList createCompletionList(Document document, XtextResource resource,
		TextDocumentPositionParams params, CancelIndicator cancelIndicator) {
	CompletionList result = new CompletionList();
	result.setIsIncomplete(false);
	// we set isInComplete to true, so we get asked always, which is the best match to the expected behavior in
	// Xtext
	XIdeContentProposalAcceptor acceptor = new XIdeContentProposalAcceptor(cancelIndicator);
	int caretOffset = document.getOffSet(params.getPosition());
	Position caretPosition = params.getPosition();
	TextRegion position = new TextRegion(caretOffset, 0);
	try {
		createProposals(document.getContents(), position, caretOffset, resource, acceptor);
	} catch (Throwable t) {
		if (!operationCanceledManager.isOperationCanceledException(t)) {
			throw t;
		}
	}
	operationCanceledManager.checkCanceled(cancelIndicator);
	IterableExtensions.forEach(acceptor.getEntries(), (it, idx) -> {
		CompletionItem item = toCompletionItem(it, caretOffset, caretPosition, document);
		item.setSortText(Strings.padStart(Integer.toString(idx.intValue()), 5, '0'));
		result.getItems().add(item);
	});
	return result;
}
 
Example 3
Source File: BatchClosureTypeTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public List<Object> resolvesClosuresTo(String expression, String... types) {
	List<XClosure> closures = findClosures(expression);
	Assert.assertFalse(closures.isEmpty());
	Assert.assertEquals(types.length, closures.size());
	IResolvedTypes resolvedTypes = getTypeResolver().resolveTypes(Iterables.getFirst(closures, null));
	List<Object> result = new ArrayList<>();
	IterableExtensions.forEach(closures, (XClosure closure, Integer index) -> {
		LightweightTypeReference closureType = resolvedTypes.getActualType(closure);
		collector.checkSucceeds(() -> {
			Assert.assertTrue("failed for closure at " + index + ": " + closureType, closureType instanceof FunctionTypeReference);
			return null;
		});
		collector.checkSucceeds(() -> {
			Assert.assertEquals("failed for closure at " + index, types[index], closureType.getSimpleName());
			return null;
		});
		result.add(closureType);
	});
	return result;
}
 
Example 4
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean visit(final VariableDeclarationExpression it) {
  final Procedure2<VariableDeclarationFragment, Integer> _function = (VariableDeclarationFragment frag, Integer counter) -> {
    this.appendModifiers(it, it.modifiers());
    this.appendToBuffer(this._aSTFlattenerUtils.handleVariableDeclaration(it.modifiers()));
    this.appendSpaceToBuffer();
    it.getType().accept(this);
    this.appendSpaceToBuffer();
    frag.accept(this);
    int _size = it.fragments().size();
    int _minus = (_size - 1);
    boolean _lessThan = ((counter).intValue() < _minus);
    if (_lessThan) {
      this.appendToBuffer(",");
      this.appendSpaceToBuffer();
    }
  };
  IterableExtensions.<VariableDeclarationFragment>forEach(it.fragments(), _function);
  return false;
}
 
Example 5
Source File: MemberFromSuperImplementor.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void initializeExecutableBuilder(final AbstractExecutableBuilder builder, final JvmDeclaredType overrider, final IResolvedExecutable overridden) {
  final JvmExecutable executable = overridden.getDeclaration();
  builder.setContext(overrider);
  builder.setVisibility(overridden.getDeclaration().getVisibility());
  final Procedure2<LightweightTypeReference, Integer> _function = (LightweightTypeReference it, Integer index) -> {
    final JvmFormalParameter declaredParameter = executable.getParameters().get((index).intValue());
    final AbstractParameterBuilder parameterBuilder = builder.newParameterBuilder();
    parameterBuilder.setName(declaredParameter.getSimpleName());
    parameterBuilder.setType(it);
    JvmAnnotationReference _findAnnotation = this.annotationLookup.findAnnotation(declaredParameter, Extension.class);
    boolean _tripleNotEquals = (_findAnnotation != null);
    parameterBuilder.setExtensionFlag(_tripleNotEquals);
  };
  IterableExtensions.<LightweightTypeReference>forEach(overridden.getResolvedParameterTypes(), _function);
  builder.setVarArgsFlag(executable.isVarArgs());
  builder.setExceptions(overridden.getResolvedExceptions());
}
 
Example 6
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(final CatchClause node) {
  int _nodeType = node.getException().getType().getNodeType();
  boolean _tripleEquals = (_nodeType == 84);
  if (_tripleEquals) {
    this.appendToBuffer(" catch (");
    final List<ASTNode> types = this._aSTFlattenerUtils.genericChildListProperty(node.getException().getType(), "types");
    if (types!=null) {
      final Procedure2<ASTNode, Integer> _function = (ASTNode child, Integer index) -> {
        child.accept(this);
        int _size = types.size();
        int _minus = (_size - 1);
        boolean _lessThan = ((index).intValue() < _minus);
        if (_lessThan) {
          this.appendSpaceToBuffer();
          this.appendToBuffer("|");
          this.appendSpaceToBuffer();
        }
      };
      IterableExtensions.<ASTNode>forEach(types, _function);
    }
    this.appendSpaceToBuffer();
    this.appendToBuffer(this._aSTFlattenerUtils.toSimpleName(node.getException().getName()));
    this.appendToBuffer(") ");
    node.getBody().accept(this);
  } else {
    this.appendToBuffer(" catch (");
    node.getException().accept(this);
    this.appendToBuffer(") ");
    node.getBody().accept(this);
  }
  return false;
}
 
Example 7
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public void visitAll(final Iterable<? extends ASTNode> iterable, final String separator) {
  final Procedure2<ASTNode, Integer> _function = (ASTNode node, Integer counter) -> {
    node.accept(this);
    if (((separator != null) && ((counter).intValue() < (IterableExtensions.size(iterable) - 1)))) {
      this.appendToBuffer(separator);
    }
  };
  IterableExtensions.forEach(iterable, _function);
}
 
Example 8
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public void acceptSyntaticBlock(final Block node) {
  final int childrenCount = node.statements().size();
  if ((childrenCount > 0)) {
    final Procedure2<ASTNode, Integer> _function = (ASTNode child, Integer counter) -> {
      child.accept(this);
      this.appendLineWrapToBuffer();
    };
    IterableExtensions.<ASTNode>forEach(node.statements(), _function);
  }
}
 
Example 9
Source File: BoundTypeArgumentMergerTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public LightweightMergedBoundTypeArgument mergeWithSource(final Object source, final Triple<String, VarianceInfo, VarianceInfo>... mergeUs) {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("def void method(");
    final Function1<Triple<String, VarianceInfo, VarianceInfo>, CharSequence> _function = (Triple<String, VarianceInfo, VarianceInfo> it) -> {
      return it.getFirst();
    };
    String _join = IterableExtensions.<Triple<String, VarianceInfo, VarianceInfo>>join(((Iterable<Triple<String, VarianceInfo, VarianceInfo>>)Conversions.doWrapArray(mergeUs)), 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);
    final ArrayList<LightweightBoundTypeArgument> mergable = CollectionLiterals.<LightweightBoundTypeArgument>newArrayList();
    final Procedure2<JvmFormalParameter, Integer> _function_1 = (JvmFormalParameter p, Integer i) -> {
      final Triple<String, VarianceInfo, VarianceInfo> input = mergeUs[(i).intValue()];
      LightweightTypeReference _lightweightTypeReference = this.toLightweightTypeReference(p.getParameterType());
      Object _elvis = null;
      if (source != null) {
        _elvis = source;
      } else {
        Object _object = new Object();
        _elvis = _object;
      }
      VarianceInfo _second = input.getSecond();
      VarianceInfo _third = input.getThird();
      LightweightBoundTypeArgument _lightweightBoundTypeArgument = new LightweightBoundTypeArgument(_lightweightTypeReference, null, _elvis, _second, _third);
      mergable.add(_lightweightBoundTypeArgument);
    };
    IterableExtensions.<JvmFormalParameter>forEach(operation.getParameters(), _function_1);
    return this.merger.merge(mergable, this.getOwner());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 10
Source File: Solution_022.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public static void main(final String[] args) {
  try {
    InputStream _resourceAsStream = Solution_022.class.getResourceAsStream("names.txt");
    InputStreamReader _inputStreamReader = new InputStreamReader(_resourceAsStream);
    final BufferedReader reader = new BufferedReader(_inputStreamReader);
    try {
      final String line = reader.readLine();
      final Matcher matcher = Pattern.compile("\"(\\w+)\"").matcher(line);
      final ArrayList<String> names = CollectionLiterals.<String>newArrayList();
      while (matcher.find()) {
        names.add(matcher.group(1));
      }
      final AtomicLong result = new AtomicLong();
      final Procedure2<String, Integer> _function = (String name, Integer i) -> {
        final Function1<Character, Integer> _function_1 = (Character it) -> {
          int _numericValue = Character.getNumericValue((it).charValue());
          return Integer.valueOf((_numericValue - 9));
        };
        final Function2<Integer, Integer, Integer> _function_2 = (Integer i1, Integer i2) -> {
          return Integer.valueOf(((i1).intValue() + (i2).intValue()));
        };
        Integer _reduce = IterableExtensions.<Integer>reduce(ListExtensions.<Character, Integer>map(((List<Character>)Conversions.doWrapArray(name.toCharArray())), _function_1), _function_2);
        int _multiply = ((_reduce).intValue() * ((i).intValue() + 1));
        result.addAndGet(_multiply);
      };
      IterableExtensions.<String>forEach(IterableExtensions.<String>sort(names), _function);
      InputOutput.<AtomicLong>println(result);
    } finally {
      reader.close();
    }
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 11
Source File: StringLiteralTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public void resolvesStringLiteralsTo(final String expression, final String... types) {
  final String expressionWithQualifiedNames = expression.replace("$$", "org::eclipse::xtext::xbase::lib::");
  final List<XStringLiteral> featureCalls = this.findLiterals(expressionWithQualifiedNames);
  Assert.assertFalse(featureCalls.isEmpty());
  Assert.assertEquals(((List<String>)Conversions.doWrapArray(types)).size(), featureCalls.size());
  final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(IterableExtensions.<XStringLiteral>head(featureCalls));
  final Procedure2<XStringLiteral, Integer> _function = (XStringLiteral featureCall, Integer index) -> {
    final LightweightTypeReference type = resolvedTypes.getActualType(featureCall);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("failed for literal at ");
    _builder.append(index);
    Assert.assertEquals(_builder.toString(), types[(index).intValue()], type.getSimpleName());
  };
  IterableExtensions.<XStringLiteral>forEach(featureCalls, _function);
}
 
Example 12
Source File: AbstractClosureTypeTest2.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public void withEquivalents(final List<Object> references, final String... types) {
  final Procedure2<Object, Integer> _function = (Object reference, Integer index) -> {
    final Callable<Object> _function_1 = () -> {
      Assert.assertTrue((reference instanceof FunctionTypeReference));
      return null;
    };
    this.collector.<Object>checkSucceeds(_function_1);
    final Callable<Object> _function_2 = () -> {
      Assert.assertEquals(types[(index).intValue()], this.getEquivalent(((FunctionTypeReference) reference)));
      return null;
    };
    this.collector.<Object>checkSucceeds(_function_2);
  };
  IterableExtensions.<Object>forEach(references, _function);
}
 
Example 13
Source File: RecomputingReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _assertEqualLinkingData(final IClosureCandidate left, final IClosureCandidate right) {
  Assert.assertEquals("type", left.getParameters().size(), right.getParameters().size());
  final Procedure2<JvmFormalParameter, Integer> _function = (JvmFormalParameter leftParam, Integer idx) -> {
    final JvmFormalParameter rightParam = right.getParameters().get((idx).intValue());
    Assert.assertEquals(leftParam.getName(), rightParam.getName());
    EStructuralFeature _eContainingFeature = leftParam.eContainingFeature();
    boolean _notEquals = (!Objects.equal(_eContainingFeature, XbasePackage.Literals.XCLOSURE__DECLARED_FORMAL_PARAMETERS));
    if (_notEquals) {
      Assert.assertEquals(leftParam.getParameterType().getIdentifier(), rightParam.getParameterType().getIdentifier());
    }
  };
  IterableExtensions.<JvmFormalParameter>forEach(left.getParameters(), _function);
}
 
Example 14
Source File: BatchTypeArgumentTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void hasTypeArguments(XExpression expression, String... typeArguments) {
	List<LightweightTypeReference> actualTypeArguments = this.resolvedTypes.getActualTypeArguments(expression);
	Assert.assertEquals(typeArguments.length, actualTypeArguments.size());
	IterableExtensions.forEach(Arrays.asList(typeArguments), (String typeArgument, Integer i) -> {
		String message = Arrays.stream(typeArguments).collect(Collectors.joining(", ")) + " vs "
				+ actualTypeArguments.stream().<String> map(a -> a.getSimpleName()).collect(Collectors.joining(", "));
		Assert.assertEquals(message, typeArgument, actualTypeArguments.get(i).getSimpleName());
	});
}
 
Example 15
Source File: BatchClosureTypeTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void withEquivalents(List<Object> references, String... types) {
	IterableExtensions.forEach(references, (Object reference, Integer index) -> {
		collector.checkSucceeds(() -> {
			Assert.assertTrue(reference instanceof FunctionTypeReference);
			return null;
		});
		collector.checkSucceeds(() -> {
			Assert.assertEquals(types[index], getEquivalent((FunctionTypeReference) reference));
			return null;
		});
	});
}
 
Example 16
Source File: BatchFeatureCallTypeTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void resolvesFeatureCallsTo(String expression, String... types) {
	String expressionWithQualifiedNames = expression.replace("$$", "org::eclipse::xtext::xbase::lib::");
	List<XAbstractFeatureCall> featureCalls = findFeatureCalls(expressionWithQualifiedNames);
	IResolvedTypes resolvedTypes = getTypeResolver().resolveTypes(Iterables.getFirst(featureCalls, null));
	Iterable<XAbstractFeatureCall> actualFeatureCalls = filterTypeLiteralsAndPackageFragments(featureCalls);
	Assert.assertFalse(Iterables.isEmpty(actualFeatureCalls));
	Assert.assertEquals("" + actualFeatureCalls, types.length, Iterables.size(actualFeatureCalls));
	IterableExtensions.forEach(actualFeatureCalls, (XAbstractFeatureCall featureCall, Integer index) -> {
		LightweightTypeReference type = resolvedTypes.getActualType(featureCall);
		Assert.assertEquals(("failed for feature call at " + index), types[index], type.getSimpleName());
	});
}
 
Example 17
Source File: BatchConstructorCallTypeTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void resolvesConstructorCallsTo(String expression, String... types) {
	String expressionWithQualifiedNames = expression.replace("$$", "org::eclipse::xtext::xbase::lib::");
	List<XConstructorCall> featureCalls = findConstructorCalls(expressionWithQualifiedNames);
	Assert.assertFalse(featureCalls.isEmpty());
	Assert.assertEquals(types.length, featureCalls.size());
	IResolvedTypes resolvedTypes = getTypeResolver().resolveTypes(Iterables.getFirst(featureCalls, null));
	IterableExtensions.forEach(featureCalls, (XConstructorCall featureCall, Integer index) -> {
		LightweightTypeReference type = resolvedTypes.getActualType(featureCall);
		Assert.assertEquals("failed for constructor call at " + index, types[index], type.getSimpleName());
	});
}
 
Example 18
Source File: IterableExtensionsTest.java    From xtext-lib with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected void forEach(Iterable<Integer> input, Procedure2<Integer, Integer> proc) {
	IterableExtensions.forEach(input, proc);
}