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 * @param nestedQuery The nested query of the join key. Can be null 035 */ 036 public JoinKey(String key, Query nestedQuery) 037 { 038 if (key == null) 039 { 040 throw new IllegalArgumentException("The key of the JoinKey cannot be null"); 041 } 042 _key = key; 043 _nestedQuery = Optional.ofNullable(nestedQuery); 044 } 045 046 /** 047 * Gets the key of the JoinKey 048 * @return the key of the JoinKey 049 */ 050 public String getKey() 051 { 052 return _key; 053 } 054 055 /** 056 * Gets the optional nested query of the join key 057 * @return the optional nested query of the join key 058 */ 059 public Optional<Query> getNestedQuery() 060 { 061 return _nestedQuery; 062 } 063 064 @Override 065 public int hashCode() 066 { 067 final int prime = 31; 068 int result = 1; 069 result = prime * result + ((_key == null) ? 0 : _key.hashCode()); 070 result = prime * result + ((_nestedQuery == null) ? 0 : _nestedQuery.hashCode()); 071 return result; 072 } 073 074 @Override 075 public boolean equals(Object obj) 076 { 077 if (this == obj) 078 { 079 return true; 080 } 081 if (obj == null) 082 { 083 return false; 084 } 085 if (getClass() != obj.getClass()) 086 { 087 return false; 088 } 089 JoinKey other = (JoinKey) obj; 090 if (_key == null) 091 { 092 if (other._key != null) 093 { 094 return false; 095 } 096 } 097 else if (!_key.equals(other._key)) 098 { 099 return false; 100 } 101 if (_nestedQuery == null) 102 { 103 if (other._nestedQuery != null) 104 { 105 return false; 106 } 107 } 108 else if (!_nestedQuery.equals(other._nestedQuery)) 109 { 110 return false; 111 } 112 return true; 113 } 114 115 @Override 116 public String toString() 117 { 118 return "JoinKey [key=" + _key + ", nestedQuery=" + _nestedQuery.map(Query::toString).orElse("<no nested query>") + "]"; 119 } 120}