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

com.speedment.jpastreamer.streamconfiguration.standard.internal.StandardStreamConfiguration Maven / Gradle / Ivy

There is a newer version: 3.0.4
Show newest version
/*
 * JPAstreamer - Express JPA queries with Java Streams
 * Copyright (c) 2020-2020, Speedment, Inc. All Rights Reserved.
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * See: https://github.com/speedment/jpa-streamer/blob/master/LICENSE
 */
package com.speedment.jpastreamer.streamconfiguration.standard.internal;

import static java.util.Objects.requireNonNull;

import com.speedment.jpastreamer.field.Field;
import com.speedment.jpastreamer.projection.Projection;
import com.speedment.jpastreamer.streamconfiguration.StreamConfiguration;

import javax.persistence.criteria.JoinType;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public final class StandardStreamConfiguration implements StreamConfiguration {

    private final Class entityClass;
    private final Projection projection;
    private final Set> joinConfigurations;

    public StandardStreamConfiguration(final Class entityClass) {
        this.entityClass = requireNonNull(entityClass);
        this.projection = null;
        this.joinConfigurations = Collections.emptySet();
    }

    private StandardStreamConfiguration(final Class entityClass, Projection projection, final Set> joinConfigurations) {
        this.entityClass = entityClass;
        this.projection = projection;
        this.joinConfigurations = new HashSet<>(joinConfigurations);
    }

    @Override
    public Class entityClass() {
        return entityClass;
    }

    @Override
    public Set> joins() {
        return Collections.unmodifiableSet(joinConfigurations);
    }

    @Override
    public StreamConfiguration joining(final Field field, final JoinType joinType) {
        requireNonNull(field);
        requireNonNull(joinType);
        final Set> newJoins = new HashSet<>(joinConfigurations);
        newJoins.add(new StandardJoinConfiguration<>(field, joinType));
        return new StandardStreamConfiguration<>(entityClass, projection, newJoins);
    }

    @Override
    public Optional> selections() {
        return Optional.ofNullable(projection);
    }

    @Override
    public StreamConfiguration selecting(Projection projection) {
        requireNonNull(projection);
        return new StandardStreamConfiguration<>(entityClass, projection, joinConfigurations);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        final StandardStreamConfiguration that = (StandardStreamConfiguration) o;

        if (!entityClass.equals(that.entityClass)) return false;
        return joinConfigurations.equals(that.joinConfigurations);
    }

    @Override
    public int hashCode() {
        int result = entityClass.hashCode();
        result = 31 * result + joinConfigurations.hashCode();
        return result;
    }

    @Override
    public String toString() {

        final String joinText = joinConfigurations.isEmpty()
                ? ""
                : " joining " + joinConfigurations.stream()
                .map(Object::toString)
                .sorted()
                .collect(Collectors.joining(", "));

        return "StandardStreamConfiguration{" +
                "of " + entityClass.getSimpleName() +
                joinText +
                '}';
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy