001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2016 ForgeRock AS.
015 */
016
017package org.forgerock.json.resource;
018
019import static org.forgerock.util.Reject.checkNotNull;
020
021/**
022 * {@link QueryResourceHandler} that searches for a specific identifier value.
023 */
024public class IdentifierQueryResourceHandler implements QueryResourceHandler {
025    private final String id;
026    private ResourceResponse resourceResponse;
027
028    /**
029     * Creates a new {@link QueryResourceHandler} for the given identifier.
030     *
031     * @param id Identifier to query for
032     */
033    public IdentifierQueryResourceHandler(final String id) {
034        this.id = checkNotNull(id);
035    }
036
037    @Override
038    public boolean handleResource(final ResourceResponse resource) {
039        if (id.equals(resource.getId())) {
040            resourceResponse = resource;
041            return false;
042        }
043        return true;
044    }
045
046    /**
047     * Gets the identifier being queried for.
048     *
049     * @return Identifier
050     */
051    public String getId() {
052        return id;
053    }
054
055    /**
056     * Gets the {@link ResourceResponse} query result.
057     *
058     * @return {@link ResourceResponse} or {@code null} if not yet found
059     */
060    public ResourceResponse getResourceResponse() {
061        return resourceResponse;
062    }
063}