001/*
002 *  Copyright 2017 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.cms.search.query.join;
017
018import java.util.Optional;
019
020import org.ametys.cms.search.query.JoinQuery;
021import org.ametys.cms.search.query.Query;
022
023/**
024 * Class representing a join key and its (optional) nested query for creating a {@link JoinQuery}
025 */
026public class JoinKey
027{
028    private String _key;
029    private Optional<Query> _nestedQuery;
030    
031    /**
032     * Creates a JoinKey
033     * @param key The key. Cannot be null
034     */
035    public JoinKey(String key)
036    {
037        this(key, null);
038    }
039    
040    /**
041     * Creates a JoinKey
042     * @param key The key. Cannot be null
043     * @param nestedQuery The nested query of the join key. Can be null
044     */
045    public JoinKey(String key, Query nestedQuery)
046    {
047        if (key == null)
048        {
049            throw new IllegalArgumentException("The key of the JoinKey cannot be null");
050        }
051        _key = key;
052        _nestedQuery = Optional.ofNullable(nestedQuery);
053    }
054    
055    /**
056     * Gets the key of the JoinKey
057     * @return the key of the JoinKey
058     */
059    public String getKey()
060    {
061        return _key;
062    }
063
064    /**
065     * Gets the optional nested query of the join key
066     * @return the optional nested query of the join key
067     */
068    public Optional<Query> getNestedQuery()
069    {
070        return _nestedQuery;
071    }
072
073    @Override
074    public int hashCode()
075    {
076        final int prime = 31;
077        int result = 1;
078        result = prime * result + ((_key == null) ? 0 : _key.hashCode());
079        result = prime * result + ((_nestedQuery == null) ? 0 : _nestedQuery.hashCode());
080        return result;
081    }
082
083    @Override
084    public boolean equals(Object obj)
085    {
086        if (this == obj)
087        {
088            return true;
089        }
090        if (obj == null)
091        {
092            return false;
093        }
094        if (getClass() != obj.getClass())
095        {
096            return false;
097        }
098        JoinKey other = (JoinKey) obj;
099        if (_key == null)
100        {
101            if (other._key != null)
102            {
103                return false;
104            }
105        }
106        else if (!_key.equals(other._key))
107        {
108            return false;
109        }
110        if (_nestedQuery == null)
111        {
112            if (other._nestedQuery != null)
113            {
114                return false;
115            }
116        }
117        else if (!_nestedQuery.equals(other._nestedQuery))
118        {
119            return false;
120        }
121        return true;
122    }
123    
124    @Override
125    public String toString()
126    {
127        return "JoinKey(key=" + _key + ",nestedQuery=" + _nestedQuery.map(Query::toString).orElse("<none>") + ")";
128    }
129}