Java Code Examples for org.eclipse.xtext.xbase.lib.Functions.Function1#apply()

The following examples show how to use org.eclipse.xtext.xbase.lib.Functions.Function1#apply() . 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: CompilationUnitImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected String getMessageWithReducedStackTrace(final Throwable t, final Function1<? super Throwable, ? extends String> getMessage) {
  final StackTraceElement[] stackTrace = t.getStackTrace();
  final ArrayList<StackTraceElement> reducedStackTrace = CollectionLiterals.<StackTraceElement>newArrayList();
  for (final StackTraceElement it : stackTrace) {
    {
      if ((it.getClassName().contains(AnnotationProcessor.class.getName()) || 
        it.getClassName().contains(ProblemSupportImpl.class.getName()))) {
        try {
          t.setStackTrace(((StackTraceElement[])Conversions.unwrapArray(reducedStackTrace, StackTraceElement.class)));
          return getMessage.apply(t);
        } finally {
          t.setStackTrace(stackTrace);
        }
      }
      reducedStackTrace.add(it);
    }
  }
  return getMessage.apply(t);
}
 
Example 2
Source File: TypeLookupImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
private <T extends Object> T recursiveFindType(final String qualifiedName, final Iterable<? extends T> typeDeclarations, final Function1<? super T, ? extends String> qualifiedNameProvider, final Function1<? super T, ? extends Iterable<? extends T>> subTypeProvider) {
  final char dot = '.';
  for (final T type : typeDeclarations) {
    {
      final String name = qualifiedNameProvider.apply(type);
      boolean _equals = Objects.equal(qualifiedName, name);
      if (_equals) {
        return type;
      }
      if ((qualifiedName.startsWith(name) && (qualifiedName.charAt(name.length()) == dot))) {
        return this.<T>recursiveFindType(qualifiedName, subTypeProvider.apply(type), qualifiedNameProvider, subTypeProvider);
      }
    }
  }
  return null;
}
 
Example 3
Source File: IteratorExtensions.java    From xtext-lib with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns a map for which the {@link Map#values} is a collection of lists, where the elements in the list will 
 * appear in the order as they appeared in the iterator. Each key is the product of invoking the supplied 
 * function {@code computeKeys} on its corresponding value. So a key of that map groups a list of values for 
 * which the function produced exactly that key. The value iterator is left exhausted.
 * 
 * @param values
 *            the values to use when constructing the {@code Map}. May not be <code>null</code>.
 * @param computeKeys
 *            the function used to produce the key for each value. May not be <code>null</code>.
 * @return a map mapping the result of evaluating the function {@code keyFunction} on each value in the input
 *         iterator to that value. As there can be more than one value mapped by a key, the mapping result is is a
 *         list of values.
 * @since 2.7
 */
public static <K, V> Map<K, List<V>> groupBy(Iterator<? extends V> values,
		Function1<? super V, ? extends K> computeKeys) {
	if (computeKeys == null)
		throw new NullPointerException("computeKeys");
	Map<K, List<V>> result = Maps.newLinkedHashMap();
	while(values.hasNext()) {
		V v = values.next();
		K key = computeKeys.apply(v);
		List<V> grouped = result.get(key);
		if (grouped == null) {
			grouped = new ArrayList<V>();
			result.put(key, grouped);
		}
		grouped.add(v);
	}
	return result;
}
 
Example 4
Source File: IteratorExtensions.java    From xtext-lib with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns an Iterator containing all elements starting from the head of the source up to and excluding the first
 * element that violates the predicate. The resulting Iterator is a lazily computed view, so any modifications to the
 * underlying Iterators will be reflected on iteration. The result does not support {@link Iterator#remove()}
 * 
 * @param iterator
 *            the elements from which to take. May not be <code>null</code>.
 * @param predicate
 *            the predicate which decides whether to keep taking elements. May not be <code>null</code>.
 * @return the taken elements
 * @since 2.7
 */
public static <T> Iterator<T> takeWhile(final Iterator<? extends T> iterator, final Function1<? super T, Boolean> predicate) {
	if (iterator == null)
		throw new NullPointerException("iterator");
	if (predicate == null)
		throw new NullPointerException("predicate");
	return new AbstractIterator<T>() {
		@Override
		protected T computeNext() {
			if (!iterator.hasNext())
				return endOfData();
			T next = iterator.next();
			if (predicate.apply(next)) {
				return next;
			} else {
				return endOfData();
			}
		}
	};
}
 
Example 5
Source File: CompilationUnitImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
private <IN extends Object, OUT extends Object> OUT getOrCreate(final IN in, final Function1<? super IN, ? extends OUT> provider) {
  this.checkCanceled();
  if ((in == null)) {
    return null;
  }
  boolean _containsKey = this.identityCache.containsKey(in);
  if (_containsKey) {
    Object _get = this.identityCache.get(in);
    return ((OUT) _get);
  }
  final OUT result = provider.apply(in);
  this.identityCache.put(in, result);
  return result;
}
 
Example 6
Source File: TemplateLiteralImpl.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public String getValueAsString() {
	final StringBuilder result = new StringBuilder("`");
	final Function1<Expression, StringBuilder> _function = new Function1<Expression, StringBuilder>() {
		public StringBuilder apply(final Expression expr) {
			StringBuilder _switchResult = null;
			boolean _matched = false;
			if (expr instanceof Literal) {
				_matched=true;
				_switchResult = result.append(TemplateLiteralImpl.this.getValueAsString());
			}
			if (!_matched) {
				_switchResult = result.append("<<").append(TemplateLiteralImpl.this.eClass().getName()).append(">>");
			}
			return _switchResult;
		}
	};
	final Function1<Expression, StringBuilder> appender = _function;
	final Function2<Boolean, Expression, Boolean> _function_1 = new Function2<Boolean, Expression, Boolean>() {
		public Boolean apply(final Boolean isRaw, final Expression expression) {
			if ((!(isRaw).booleanValue())) {
				result.append("${");
				appender.apply(expression);
				result.append("}");
			}
			else {
				appender.apply(expression);
			}
			return Boolean.valueOf((!(isRaw).booleanValue()));
		}
	};
	IterableExtensions.<Expression, Boolean>fold(this.getSegments(), Boolean.valueOf(true), _function_1);
	result.append("`");
	return result.toString();
}
 
Example 7
Source File: Case_3.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public void testReturnExpression_07(final Class<?> c) {
  boolean _notEquals = (!Objects.equal(Case_3.class, c));
  if (_notEquals) {
    final Function1<Integer, Integer> _function = (Integer i) -> {
      return (i).intValue();
    };
    final Function1<? super Integer, ? extends Integer> closure = _function;
    IntegerRange _upTo = new IntegerRange(1, 100);
    for (final Integer x : _upTo) {
      closure.apply(x);
    }
  }
}
 
Example 8
Source File: Case_3.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public void testReturnExpression_06() {
  final Function1<Integer, Integer> _function = (Integer i) -> {
    return i;
  };
  final Function1<? super Integer, ? extends Integer> closure = _function;
  IntegerRange _upTo = new IntegerRange(1, 100);
  for (final Integer x : _upTo) {
    closure.apply(x);
  }
}
 
Example 9
Source File: JavaEditorExtension.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public ITextEditor close(final ITextEditor editor, final Function1<? super ITextEditor, ? extends ITextEditor> consumer) {
  ITextEditor _xblockexpression = null;
  {
    final Procedure0 _function = () -> {
      if ((consumer != null)) {
        consumer.apply(editor);
      }
      Assert.assertTrue(this._workbenchTestHelper.closeEditor(editor, false));
    };
    this.waitForPostChangeEvent(_function);
    _xblockexpression = editor;
  }
  return _xblockexpression;
}
 
Example 10
Source File: JavaEditorExtension.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public ITextEditor save(final ITextEditor editor, final Function1<? super ITextEditor, ? extends ITextEditor> consumer) {
  ITextEditor _xblockexpression = null;
  {
    final Procedure0 _function = () -> {
      if ((consumer != null)) {
        consumer.apply(editor);
      }
      Assert.assertTrue(this._workbenchTestHelper.saveEditor(editor, false));
    };
    this.waitForPostChangeEvent(_function);
    _xblockexpression = editor;
  }
  return _xblockexpression;
}
 
Example 11
Source File: JavaEditorExtension.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public ITextEditor reconcile(final ITextEditor editor, final Function1<? super ITextEditor, ? extends ITextEditor> consumer) {
  ITextEditor _xblockexpression = null;
  {
    if ((consumer == null)) {
      return editor;
    }
    final Procedure0 _function = () -> {
      consumer.apply(editor);
    };
    this.waitForPostReconcileEvent(_function);
    _xblockexpression = editor;
  }
  return _xblockexpression;
}
 
Example 12
Source File: FunctionExtensions.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Curries a function that takes one argument.
 * 
 * @param function
 *            the original function. May not be <code>null</code>.
 * @param argument
 *            the fixed argument.
 * @return a function that takes no arguments. Never <code>null</code>.
 */
@Pure
public static <P1, RESULT> Function0<RESULT> curry(final Function1<? super P1, ? extends RESULT> function, final P1 argument) {
	if (function == null)
		throw new NullPointerException("function");
	return new Function0<RESULT>() {
		@Override
		public RESULT apply() {
			return function.apply(argument);
		}
	};
}
 
Example 13
Source File: IteratorExtensions.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns an Iterator containing all elements starting from the first element for which the drop-predicate returned
 * false. The resulting Iterator is a lazily computed view, so any modifications to the
 * underlying Iterators will be reflected on iteration. The result does not support {@link Iterator#remove()}
 * 
 * @param iterator
 *            the elements from which to drop. May not be <code>null</code>.
 * @param predicate
 *            the predicate which decides whether to keep dropping elements. May not be <code>null</code>.
 * @return the remaining elements after dropping
 * @since 2.7
 */
public static <T> Iterator<T> dropWhile(final Iterator<? extends T> iterator, final Function1<? super T, Boolean> predicate) {
	if (iterator == null)
		throw new NullPointerException("iterator");
	if (predicate == null)
		throw new NullPointerException("predicate");
	return new AbstractIterator<T>() {
		private boolean headFound = false;

		@Override
		protected T computeNext() {
			while (!headFound) {
				if (!iterator.hasNext())
					return endOfData();
				T next = iterator.next();
				if (!predicate.apply(next)) {
					headFound = true;
					return next;
				}
			}
			if (iterator.hasNext()) {
				return iterator.next();
			} else {
				return endOfData();
			}
		}
	};
}
 
Example 14
Source File: AbstractTraceRegionToString.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected <R extends Collection<? super T>, T extends Object> R collect(T start,
		Function1<? super T, ? extends Iterable<T>> reachable, R collector) {
	if (collector.add(start)) {
		for (T r : reachable.apply(start)) {
			collect(r, reachable, collector);
		}
	}
	return collector;
}
 
Example 15
Source File: FormattingDataFactory.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public Function1<? super FormattableDocument, ? extends Iterable<FormattingData>> surround(final INode node, final Procedure1<? super FormattingDataInit> before, final Procedure1<? super FormattingDataInit> after) {
  final Function1<FormattableDocument, Iterable<FormattingData>> _function = (FormattableDocument doc) -> {
    ArrayList<FormattingData> _xblockexpression = null;
    {
      final ArrayList<FormattingData> result = CollectionLiterals.<FormattingData>newArrayList();
      if ((node != null)) {
        Iterable<FormattingData> _elvis = null;
        Function1<? super FormattableDocument, ? extends Iterable<FormattingData>> _newFormattingData = this.newFormattingData(this._hiddenLeafAccess.getHiddenLeafsBefore(node), before);
        Iterable<FormattingData> _apply = null;
        if (_newFormattingData!=null) {
          _apply=_newFormattingData.apply(doc);
        }
        if (_apply != null) {
          _elvis = _apply;
        } else {
          List<FormattingData> _emptyList = CollectionLiterals.<FormattingData>emptyList();
          _elvis = _emptyList;
        }
        Iterables.<FormattingData>addAll(result, _elvis);
        Iterable<FormattingData> _elvis_1 = null;
        Function1<? super FormattableDocument, ? extends Iterable<FormattingData>> _newFormattingData_1 = this.newFormattingData(this._hiddenLeafAccess.getHiddenLeafsAfter(node), after);
        Iterable<FormattingData> _apply_1 = null;
        if (_newFormattingData_1!=null) {
          _apply_1=_newFormattingData_1.apply(doc);
        }
        if (_apply_1 != null) {
          _elvis_1 = _apply_1;
        } else {
          List<FormattingData> _emptyList_1 = CollectionLiterals.<FormattingData>emptyList();
          _elvis_1 = _emptyList_1;
        }
        Iterables.<FormattingData>addAll(result, _elvis_1);
      }
      _xblockexpression = result;
    }
    return _xblockexpression;
  };
  return _function;
}
 
Example 16
Source File: FormattingDataFactory.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public Function1<? super FormattableDocument, ? extends Iterable<FormattingData>> surround(final INode node, final Procedure1<? super FormattingDataInit> init) {
  final Function1<FormattableDocument, Iterable<FormattingData>> _function = (FormattableDocument doc) -> {
    ArrayList<FormattingData> _xblockexpression = null;
    {
      final ArrayList<FormattingData> result = CollectionLiterals.<FormattingData>newArrayList();
      if ((node != null)) {
        Iterable<FormattingData> _elvis = null;
        Function1<? super FormattableDocument, ? extends Iterable<FormattingData>> _newFormattingData = this.newFormattingData(this._hiddenLeafAccess.getHiddenLeafsBefore(node), init);
        Iterable<FormattingData> _apply = null;
        if (_newFormattingData!=null) {
          _apply=_newFormattingData.apply(doc);
        }
        if (_apply != null) {
          _elvis = _apply;
        } else {
          List<FormattingData> _emptyList = CollectionLiterals.<FormattingData>emptyList();
          _elvis = _emptyList;
        }
        Iterables.<FormattingData>addAll(result, _elvis);
        Iterable<FormattingData> _elvis_1 = null;
        Function1<? super FormattableDocument, ? extends Iterable<FormattingData>> _newFormattingData_1 = this.newFormattingData(this._hiddenLeafAccess.getHiddenLeafsAfter(node), init);
        Iterable<FormattingData> _apply_1 = null;
        if (_newFormattingData_1!=null) {
          _apply_1=_newFormattingData_1.apply(doc);
        }
        if (_apply_1 != null) {
          _elvis_1 = _apply_1;
        } else {
          List<FormattingData> _emptyList_1 = CollectionLiterals.<FormattingData>emptyList();
          _elvis_1 = _emptyList_1;
        }
        Iterables.<FormattingData>addAll(result, _elvis_1);
      }
      _xblockexpression = result;
    }
    return _xblockexpression;
  };
  return _function;
}
 
Example 17
Source File: UnicodeTerminalsGenerator.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
public static StringWriter generateUnicodeRules(final Function1<? super Integer, ? extends Boolean> guard) {
  Character prev = null;
  boolean run = false;
  boolean first = true;
  char c = Character.MIN_VALUE;
  final StringWriter result = new StringWriter();
  final PrintWriter printer = new PrintWriter(result, true);
  while ((c != Character.MAX_VALUE)) {
    {
      Boolean _apply = guard.apply(Integer.valueOf(((int) c)));
      if ((_apply).booleanValue()) {
        if ((!run)) {
          prev = Character.valueOf(c);
          run = true;
        }
      } else {
        if (run) {
          if ((!first)) {
            printer.print("| ");
          } else {
            printer.print("  ");
            first = false;
          }
          String _padStart = Strings.padStart(Integer.toHexString((prev).charValue()).toUpperCase(), 4, '0');
          String _plus = ("\'\\u" + _padStart);
          String _plus_1 = (_plus + "\'");
          printer.print(_plus_1);
          char _charValue = prev.charValue();
          boolean _equals = (_charValue == (c - 1));
          if (_equals) {
            printer.println();
          } else {
            String _padStart_1 = Strings.padStart(Integer.toHexString((c - 1)).toUpperCase(), 4, '0');
            String _plus_2 = ("..\'\\u" + _padStart_1);
            String _plus_3 = (_plus_2 + "\'");
            printer.println(_plus_3);
          }
          prev = null;
          run = false;
        }
      }
      c = ((char) (c + 1));
    }
  }
  return result;
}
 
Example 18
Source File: FunctionExtensions.java    From xtext-lib with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Returns a composed function that first applies the {@code before}
 * function to its input, and then applies the {@code after} function to the result.
 * If evaluation of either function throws an exception, it is relayed to
 * the caller of the composed function.
 *
 * @param <V1> the type of the first parameter to the {@code before} function, and to the composed function
 * @param <V2> the type of the second parameter to the {@code before} function, and to the composed function
 * @param <T> the type of output of the {@code before} function, and input to the {@code after} function
 * @param <R> the type of output to the {@code after} function, and to the composed function
 * @param before the function to apply before the {@code after} function is applied
 * @param after the function to apply after the {@code before} function is applied
 * @return a composed function that first applies the {@code before}
 * function and then applies the {@code after} function
 * @throws NullPointerException if {@code before} or {@code after} is null
 *
 * @see #compose(Functions.Function1, Functions.Function1)
 * @since 2.9
 */
public static <V1,V2,T,R> Function2<V1, V2, R> andThen(final Function2<? super V1,? super V2, ? extends T> before, final Function1<? super T, ? extends R> after) {
	if (after == null)
		throw new NullPointerException("after");
	if (before == null)
		throw new NullPointerException("before");
	return new Function2<V1, V2, R>() {
		@Override
		public R apply(V1 v1, V2 v2) {
			return after.apply(before.apply(v1, v2));
		}
	};
}
 
Example 19
Source File: IteratorExtensions.java    From xtext-lib with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Returns {@code true} if every element in {@code iterator} satisfies the predicate. If {@code iterator} is empty,
 * {@code true} is returned. In other words, <code>false</code> is returned if at least one element fails to fulfill
 * the predicate.
 * 
 * @param iterator
 *            the iterator. May not be <code>null</code>.
 * @param predicate
 *            the predicate. May not be <code>null</code>.
 * @return <code>true</code> if every element in {@code iterator} satisfies the predicate and also if there is no element.
 */
public static <T> boolean forall(Iterator<T> iterator, Function1<? super T, Boolean> predicate) {
	if (predicate == null)
		throw new NullPointerException("predicate");
	while(iterator.hasNext()) {
		if (!predicate.apply(iterator.next()))
			return false;
	}
	return true;
}
 
Example 20
Source File: IteratorExtensions.java    From xtext-lib with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Finds the first element in the given iterator that fulfills the predicate. If none is found or the iterator is
 * empty, <code>null</code> is returned.
 * 
 * @param iterator
 *            the iterator. May not be <code>null</code>.
 * @param predicate
 *            the predicate. May not be <code>null</code>.
 * @return the first element in the iterator for which the given predicate returns <code>true</code>, returns
 *         <code>null</code> if no element matches the predicate or the iterator is empty.
 */
public static <T> T findFirst(Iterator<T> iterator, Function1<? super T, Boolean> predicate) {
	if (predicate == null)
		throw new NullPointerException("predicate");
	while(iterator.hasNext()) {
		T t = iterator.next();
		if (predicate.apply(t))
			return t;
	}
	return null;
}