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

org.modeshape.jcr.query.validate.ImmutableKey Maven / Gradle / Ivy

There is a newer version: 5.4.1.Final
Show newest version
/*
 * ModeShape (http://www.modeshape.org)
 *
 * 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 org.modeshape.jcr.query.validate;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.modeshape.jcr.query.validate.Schemata.Column;
import org.modeshape.jcr.query.validate.Schemata.Key;

/**
 * 
 */
public class ImmutableKey implements Key {

    private final Set columns;

    protected ImmutableKey( Iterable columns ) {
        assert columns != null;
        Set columnSet = new HashSet();
        for (Column column : columns) {
            if (column != null) columnSet.add(column);
        }
        assert !columnSet.isEmpty();
        this.columns = Collections.unmodifiableSet(columnSet);
    }

    protected ImmutableKey( Column... columns ) {
        assert columns != null;
        assert columns.length > 0;
        Set columnSet = new HashSet();
        for (Column column : columns) {
            if (column != null) columnSet.add(column);
        }
        assert !columnSet.isEmpty();
        this.columns = Collections.unmodifiableSet(columnSet);
    }

    @Override
    public Set getColumns() {
        return columns;
    }

    @Override
    public boolean hasColumns( Column... columns ) {
        Set keyColumns = new HashSet(this.columns);
        for (Column expected : columns) {
            if (!keyColumns.remove(expected)) return false;
        }
        return keyColumns.isEmpty();
    }

    @Override
    public boolean hasColumns( Iterable columns ) {
        Set keyColumns = new HashSet(this.columns);
        for (Column expected : columns) {
            if (!keyColumns.remove(expected)) return false;
        }
        return keyColumns.isEmpty();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        sb.append('[');
        for (Column column : columns) {
            if (first) first = false;
            else sb.append(", ");
            sb.append(column);
        }
        sb.append(']');
        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy