All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.azure.resourcemanager.mediaservices.implementation.Utils Maven / Gradle / Ivy

Go to download

This package contains Microsoft Azure SDK for MediaServices Management SDK. For documentation on how to use this package, please see https://aka.ms/azsdk/java/mgmt. This Swagger was generated by the API Framework. Package tag package-account-2023-01.

There is a newer version: 2.4.0-beta.2
Show newest version
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.mediaservices.implementation;

import com.azure.core.http.rest.PagedFlux;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.PagedResponse;
import com.azure.core.http.rest.PagedResponseBase;
import com.azure.core.util.CoreUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import reactor.core.publisher.Flux;

final class Utils {
    static String getValueFromIdByName(String id, String name) {
        if (id == null) {
            return null;
        }
        Iterator itr = Arrays.stream(id.split("/")).iterator();
        while (itr.hasNext()) {
            String part = itr.next();
            if (part != null && !part.trim().isEmpty()) {
                if (part.equalsIgnoreCase(name)) {
                    if (itr.hasNext()) {
                        return itr.next();
                    } else {
                        return null;
                    }
                }
            }
        }
        return null;
    }

    static String getValueFromIdByParameterName(String id, String pathTemplate, String parameterName) {
        if (id == null || pathTemplate == null) {
            return null;
        }
        String parameterNameParentheses = "{" + parameterName + "}";
        List idSegmentsReverted = Arrays.asList(id.split("/"));
        List pathSegments = Arrays.asList(pathTemplate.split("/"));
        Collections.reverse(idSegmentsReverted);
        Iterator idItrReverted = idSegmentsReverted.iterator();
        int pathIndex = pathSegments.size();
        while (idItrReverted.hasNext() && pathIndex > 0) {
            String idSegment = idItrReverted.next();
            String pathSegment = pathSegments.get(--pathIndex);
            if (!CoreUtils.isNullOrEmpty(idSegment) && !CoreUtils.isNullOrEmpty(pathSegment)) {
                if (pathSegment.equalsIgnoreCase(parameterNameParentheses)) {
                    if (pathIndex == 0 || (pathIndex == 1 && pathSegments.get(0).isEmpty())) {
                        List segments = new ArrayList<>();
                        segments.add(idSegment);
                        idItrReverted.forEachRemaining(segments::add);
                        Collections.reverse(segments);
                        if (segments.size() > 0 && segments.get(0).isEmpty()) {
                            segments.remove(0);
                        }
                        return String.join("/", segments);
                    } else {
                        return idSegment;
                    }
                }
            }
        }
        return null;
    }

    static  PagedIterable mapPage(PagedIterable pageIterable, Function mapper) {
        return new PagedIterableImpl(pageIterable, mapper);
    }

    private static final class PagedIterableImpl extends PagedIterable {

        private final PagedIterable pagedIterable;
        private final Function mapper;
        private final Function, PagedResponse> pageMapper;

        private PagedIterableImpl(PagedIterable pagedIterable, Function mapper) {
            super(
                PagedFlux
                    .create(
                        () ->
                            (continuationToken, pageSize) ->
                                Flux.fromStream(pagedIterable.streamByPage().map(getPageMapper(mapper)))));
            this.pagedIterable = pagedIterable;
            this.mapper = mapper;
            this.pageMapper = getPageMapper(mapper);
        }

        private static  Function, PagedResponse> getPageMapper(Function mapper) {
            return page ->
                new PagedResponseBase(
                    page.getRequest(),
                    page.getStatusCode(),
                    page.getHeaders(),
                    page.getElements().stream().map(mapper).collect(Collectors.toList()),
                    page.getContinuationToken(),
                    null);
        }

        @Override
        public Stream stream() {
            return pagedIterable.stream().map(mapper);
        }

        @Override
        public Stream> streamByPage() {
            return pagedIterable.streamByPage().map(pageMapper);
        }

        @Override
        public Stream> streamByPage(String continuationToken) {
            return pagedIterable.streamByPage(continuationToken).map(pageMapper);
        }

        @Override
        public Stream> streamByPage(int preferredPageSize) {
            return pagedIterable.streamByPage(preferredPageSize).map(pageMapper);
        }

        @Override
        public Stream> streamByPage(String continuationToken, int preferredPageSize) {
            return pagedIterable.streamByPage(continuationToken, preferredPageSize).map(pageMapper);
        }

        @Override
        public Iterator iterator() {
            return new IteratorImpl(pagedIterable.iterator(), mapper);
        }

        @Override
        public Iterable> iterableByPage() {
            return new IterableImpl, PagedResponse>(pagedIterable.iterableByPage(), pageMapper);
        }

        @Override
        public Iterable> iterableByPage(String continuationToken) {
            return new IterableImpl, PagedResponse>(
                pagedIterable.iterableByPage(continuationToken), pageMapper);
        }

        @Override
        public Iterable> iterableByPage(int preferredPageSize) {
            return new IterableImpl, PagedResponse>(
                pagedIterable.iterableByPage(preferredPageSize), pageMapper);
        }

        @Override
        public Iterable> iterableByPage(String continuationToken, int preferredPageSize) {
            return new IterableImpl, PagedResponse>(
                pagedIterable.iterableByPage(continuationToken, preferredPageSize), pageMapper);
        }
    }

    private static final class IteratorImpl implements Iterator {

        private final Iterator iterator;
        private final Function mapper;

        private IteratorImpl(Iterator iterator, Function mapper) {
            this.iterator = iterator;
            this.mapper = mapper;
        }

        @Override
        public boolean hasNext() {
            return iterator.hasNext();
        }

        @Override
        public S next() {
            return mapper.apply(iterator.next());
        }

        @Override
        public void remove() {
            iterator.remove();
        }
    }

    private static final class IterableImpl implements Iterable {

        private final Iterable iterable;
        private final Function mapper;

        private IterableImpl(Iterable iterable, Function mapper) {
            this.iterable = iterable;
            this.mapper = mapper;
        }

        @Override
        public Iterator iterator() {
            return new IteratorImpl(iterable.iterator(), mapper);
        }
    }
}