Example usage for java.util.stream IntStream range

List of usage examples for java.util.stream IntStream range

Introduction

In this page you can find the example usage for java.util.stream IntStream range.

Prototype

public static IntStream range(int startInclusive, int endExclusive) 

Source Link

Document

Returns a sequential ordered IntStream from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1 .

Usage

From source file:com.dgtlrepublic.anitomyj.Tokenizer.java

/**
 * Tokenize by Delimiters allowed in {@link Options#allowedDelimiters}
 *
 * @param enclosed whether or not the current {@code range} is enclosed in braces
 * @param range    the token range/*from  w ww .  j  a v a2  s .  c  o  m*/
 */
private void tokenizeByDelimiters(boolean enclosed, TokenRange range) {
    String delimiters = getDelimiters(range);

    if (delimiters.isEmpty()) {
        addToken(TokenCategory.kUnknown, enclosed, range);
        return;
    }

    for (int i = range.getOffset(), end = range.getOffset() + range.getSize(); i < end;) {
        Integer found = IntStream.range(i, Math.min(end, filename.length()))
                .filter(c -> StringUtils.containsAny(String.valueOf(filename.charAt(c)), delimiters))
                .findFirst().orElse(end);

        TokenRange subrange = new TokenRange(i, found - i);
        if (subrange.getSize() > 0) {
            addToken(TokenCategory.kUnknown, enclosed, subrange);
        }

        if (found != end) {
            addToken(TokenCategory.kDelimiter, enclosed,
                    new TokenRange(subrange.getOffset() + subrange.getSize(), 1));
            i = found + 1;
        } else {
            break;
        }
    }

    validateDelimiterTokens();
}

From source file:org.lightjason.agentspeak.action.buildin.math.statistic.CCreateDistribution.java

@Override
public final IFuzzyValue<Boolean> execute(final IContext p_context, final boolean p_parallel,
        final List<ITerm> p_argument, final List<ITerm> p_return, final List<ITerm> p_annotation) {
    final List<ITerm> l_arguments = CCommon.flatcollection(p_argument).collect(Collectors.toList());

    IntStream.range(0, l_arguments.size())
            .filter(i -> CCommon.rawvalueAssignableTo(l_arguments.get(i), String.class))
            .mapToObj(i -> new AbstractMap.SimpleImmutableEntry<>(i, l_arguments.get(i).<String>raw()))
            .filter(i -> EDistribution.exist(i.getValue()))
            .map(i -> new AbstractMap.SimpleImmutableEntry<>(i.getKey(), EDistribution.from(i.getValue())))
            .map(i -> {/*w w  w  . j a  va 2 s  .c om*/

                // check if next argument to the distribution name a generator name
                final int l_skip;
                final EGenerator l_generator;

                if ((i.getKey() < l_arguments.size() - 1)
                        && (CCommon.rawvalueAssignableTo(l_arguments.get(i.getKey() + 1), String.class))) {
                    l_skip = 1;
                    l_generator = EGenerator.from(l_arguments.get(i.getKey() + 1).<String>raw());
                } else {
                    l_skip = 0;
                    l_generator = EGenerator.MERSENNETWISTER;
                }

                // generate distribution object, arguments after distribution are the initialize parameter
                return i.getValue().get(l_generator.get(),
                        l_arguments.stream().skip(i.getKey() + 1 + l_skip)
                                .limit(i.getValue().getArgumentNumber()).map(ITerm::<Number>raw)
                                .mapToDouble(Number::doubleValue).toArray());

            }).map(CRawTerm::from).forEach(p_return::add);

    return CFuzzyValue.from(true);
}

From source file:kishida.cnn.layers.ConvolutionLayer.java

@Override
public void joinBatch() {
    float count = parent.getMiniBatch();
    IntStream.range(0, filter.length).parallel().forEach(i -> filter[i] += filterDelta[i] / count
            - parent.getWeightDecay() * parent.getLearningRate() * filter[i]);
    IntStream.range(0, bias.length).parallel().forEach(i -> bias[i] += biasDelta[i] / count);
}

From source file:fi.helsinki.opintoni.service.favorite.FavoriteService.java

public void createDefaultFavorites(final User user) {
    List<Favorite> favorites = Lists.newArrayList();

    List<Map<String, String>> defaultFavorites = favoriteProperties.getDefaultFavorites();

    defaultFavorites.stream().forEach(f -> createFavorite(f, favorites));

    IntStream.range(0, favorites.size()).forEach(index -> {
        Favorite favorite = favorites.get(index);
        favorite.user = user;/* w w  w  .j a  va2  s. c  o m*/
        favorite.orderIndex = index;
        favoriteRepository.save(favorite);
    });
}

From source file:com.github.aptd.simulation.CMain.java

/**
 * returns the experiment data models/*from  www. j  a v a2 s.  c o  m*/
 *
 * @param p_options commandline options
 * @return stream of experiments
 */
private static Stream<Pair<EDataModel, String>> datamodel(final CommandLine p_options) {
    final List<String> l_instances = Arrays.stream(p_options.getOptionValue("scenario").split(","))
            .map(String::trim).filter(i -> !i.isEmpty()).collect(Collectors.toList());

    final List<String> l_types = Arrays.stream(p_options.getOptionValue("scenariotype", "").split(","))
            .map(String::trim).filter(i -> !i.isEmpty()).collect(Collectors.toList());

    return StreamUtils.zip(l_instances.stream(),
            Stream.concat(l_types.stream(),
                    IntStream.range(0, l_instances.size() - l_types.size()).mapToObj(
                            i -> CConfiguration.INSTANCE.getOrDefault("xml", "default", "datamodel"))),
            (i, j) -> new ImmutablePair<>(EDataModel.from(j), i));
}

From source file:com.devicehive.service.DeviceNotificationServiceTest.java

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
public void testFindWithEmptyResponse() throws Exception {
    final List<String> guids = IntStream.range(0, 5).mapToObj(i -> UUID.randomUUID().toString())
            .collect(Collectors.toList());
    final Date timestampSt = new Date();
    final Date timestampEnd = new Date();

    final Set<String> guidsForSearch = new HashSet<>(Arrays.asList(guids.get(0), guids.get(2), guids.get(3)));

    // return empty response for any request
    when(requestHandler.handle(any(Request.class))).thenReturn(Response.newBuilder()
            .withBody(new NotificationSearchResponse(Collections.emptyList())).buildSuccess());

    notificationService.find(guidsForSearch, Collections.emptySet(), timestampSt, timestampEnd)
            .thenAccept(notifications -> assertTrue(notifications.isEmpty())).exceptionally(ex -> {
                fail(ex.toString());/*from  ww  w .java2  s .c  o m*/
                return null;
            }).get(15, TimeUnit.SECONDS);

    verify(requestHandler, times(3)).handle(argument.capture());

    NotificationSearchRequest request = argument.getValue().getBody().cast(NotificationSearchRequest.class);
    assertNull(request.getId());
    assertTrue(guidsForSearch.contains(request.getGuid()));
    assertTrue(request.getNames().isEmpty());
    assertEquals(timestampSt, request.getTimestampStart());
    assertEquals(timestampEnd, request.getTimestampEnd());
}

From source file:org.lightjason.agentspeak.common.CPath.java

@Override
public final IPath getSubPath(final int p_fromIndex, final int p_toIndex) {
    return new CPath(p_toIndex == 0 ? Stream.of()
            : IntStream.range(p_fromIndex, p_toIndex > 0 ? p_toIndex : this.size() + p_toIndex)
                    .mapToObj(m_path::get)).setSeparator(m_separator);
}

From source file:org.lightjason.agentspeak.action.builtin.math.statistic.CCreateDistribution.java

@Nonnull
@Override//from   w w  w.  j ava 2 s  .  co  m
public final IFuzzyValue<Boolean> execute(final boolean p_parallel, @Nonnull final IContext p_context,
        @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return) {
    final List<ITerm> l_arguments = CCommon.flatten(p_argument).collect(Collectors.toList());

    IntStream.range(0, l_arguments.size())
            .filter(i -> CCommon.rawvalueAssignableTo(l_arguments.get(i), String.class))
            .mapToObj(i -> new AbstractMap.SimpleImmutableEntry<>(i, l_arguments.get(i).<String>raw()))
            .filter(i -> EDistribution.exist(i.getValue()))
            .map(i -> new AbstractMap.SimpleImmutableEntry<>(i.getKey(), EDistribution.from(i.getValue())))
            .map(i -> {

                // check if next argument to the distribution name a generator name
                final int l_skip;
                final EGenerator l_generator;

                if ((i.getKey() < l_arguments.size() - 1)
                        && (CCommon.rawvalueAssignableTo(l_arguments.get(i.getKey() + 1), String.class))) {
                    l_skip = 1;
                    l_generator = EGenerator.from(l_arguments.get(i.getKey() + 1).<String>raw());
                } else {
                    l_skip = 0;
                    l_generator = EGenerator.MERSENNETWISTER;
                }

                // generate distribution object, arguments after distribution are the initialize parameter
                return i.getValue().get(l_generator.get(),
                        l_arguments.stream().skip(i.getKey() + 1 + l_skip)
                                .limit(i.getValue().getArgumentNumber()).map(ITerm::<Number>raw)
                                .mapToDouble(Number::doubleValue).toArray());

            }).map(CRawTerm::from).forEach(p_return::add);

    return CFuzzyValue.from(true);
}

From source file:org.datalorax.populace.core.util.TypeResolver.java

/**
 * Get a stream of available alias for the provided type variable e.g. given a type:
 * <pre>//from w  w  w .  j a  va 2  s  .c  o m
 * {@code
 *    interface SomeInterface&lt;T2&gt; {}
 *    class SomeType&lt;T2&gt; implements SomeInterface&lt;T2&gt; {}
 *
 *    TypeVariable&lt;?&gt; typeVariable = SomeType.class.getTypeParameters()[0];
 *    TypeToken interfaceToken = findTypeToken(TypeToken.of(SomeType.class).getTypes(), SomeInterface.class);
 *    getTypeArgumentAliases(typeVariable, interfaceToken);
 * }
 * </pre>
 * <p>
 * will return a stream containing T2.
 *
 * @param typeVar the type variable to find an alias for
 * @param type    the type token to search for matching type variables
 * @return the type argument of any matching type variables found.
 */
private static Stream<TypeVariable<?>> getTypeArgumentAliases(final TypeVariable<?> typeVar,
        final TypeToken<?> type) {
    Validate.isInstanceOf(ParameterizedType.class, type.getType());
    final Type[] sourceTypeArgs = ((ParameterizedType) type.getType()).getActualTypeArguments();
    final TypeVariable<? extends Class<?>>[] sourceAliases = type.getRawType().getTypeParameters();

    return IntStream.range(0, sourceTypeArgs.length).filter(i -> sourceTypeArgs[i].equals(typeVar)) // Filter out indexes with different type argument
            .filter(i -> !sourceAliases[i].equals(typeVar)) // Filter out aliases that match existing
            .mapToObj(i -> sourceAliases[i]); // Return aliases
}