blob: c42dfd0f17b86adb51b3acddf41c77346a6709f3 [file] [log] [blame]
/*
* Copyright 2017 Kuelap, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.mifos.portfolio.service.internal.mapper;
import io.mifos.portfolio.api.v1.domain.BalanceSegmentSet;
import io.mifos.portfolio.service.internal.repository.BalanceSegmentSetEntity;
import io.mifos.portfolio.service.internal.repository.ProductEntity;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author Myrle Krantz
*/
public class BalanceSegmentSetMapper {
public static List<BalanceSegmentSetEntity> map(final BalanceSegmentSet instance, final ProductEntity product) {
return
Stream.iterate(0, i -> i+1).limit(instance.getSegmentIdentifiers().size())
.map(i -> {
final BalanceSegmentSetEntity ret = new BalanceSegmentSetEntity();
ret.setProduct(product);
ret.setSegmentSetIdentifier(instance.getIdentifier());
ret.setSegmentIdentifier(instance.getSegmentIdentifiers().get(i));
ret.setLowerBound(instance.getSegments().get(i));
return ret;
})
.collect(Collectors.toList());
}
public static Optional<BalanceSegmentSet> map(final Stream<BalanceSegmentSetEntity> instances) {
final BalanceSegmentSet ret = new BalanceSegmentSet();
ret.setSegments(new ArrayList<>());
ret.setSegmentIdentifiers(new ArrayList<>());
instances.sorted(Comparator.comparing(BalanceSegmentSetEntity::getLowerBound))
.forEach(seg -> {
ret.setIdentifier(seg.getSegmentSetIdentifier());
ret.getSegments().add(seg.getLowerBound());
ret.getSegmentIdentifiers().add(seg.getSegmentIdentifier());
});
if (ret.getSegments().isEmpty())
return Optional.empty();
else
return Optional.of(ret);
}
}