Changeset 7076


Ignore:
Timestamp:
11/11/16 15:17:49 (8 years ago)
Author:
Oliver Schonefeld
Message:
  • add methods to get extra response data by class type
File:
1 edited

Legend:

Unmodified
Added
Removed
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUAbstractResponse.java

    r6938 r7076  
    1717package eu.clarin.sru.client;
    1818
     19import java.util.ArrayList;
    1920import java.util.Collections;
    2021import java.util.List;
     
    147148
    148149    /**
     150     * Get the extra response data of a specific class for this result.
     151     *
     152     * @param clazz
     153     *            the specific class to check for
     154     * @param <V>
     155     *            the type of {@link SRUExtraResponseData} to check for
     156     *
     157     * @return a {@link SRUExtraResponseData} instances for the
     158     *         extra response data from the SRU response or <code>null</code> if
     159     *         none are available
     160     */
     161    public <V extends SRUExtraResponseData> List<V> getExtraResponseData(
     162            Class<V> clazz) {
     163        if (clazz == null) {
     164            throw new NullPointerException("clazz == null");
     165        }
     166        List<V> result = null;
     167        for (SRUExtraResponseData i : extraResponseData) {
     168            if (clazz.isInstance(i)) {
     169                if (result == null) {
     170                    result = new ArrayList<V>();
     171                }
     172                result.add(clazz.cast(i));
     173            }
     174        }
     175        if (result != null) {
     176            return Collections.unmodifiableList(result);
     177        } else {
     178            return null;
     179        }
     180    }
     181
     182
     183    /**
     184     * Get the first instance of extra response data of a specific class for
     185     * this result.
     186     *
     187     * @param clazz
     188     *            the specific class to check for
     189     * @param <V>
     190     *            the type of {@link SRUExtraResponseData} to check for
     191     * @return a list of {@link SRUExtraResponseData} instances for the extra
     192     *         response data from the SRU response or <code>null</code> if none
     193     *         are available
     194     */
     195    public <V extends SRUExtraResponseData> V getFirstExtraResponseData(
     196            Class<V> clazz) {
     197        if (clazz == null) {
     198            throw new NullPointerException("clazz == null");
     199        }
     200        for (SRUExtraResponseData i : extraResponseData) {
     201            if (clazz.isInstance(i)) {
     202                return clazz.cast(i);
     203            }
     204        }
     205        return null;
     206    }
     207
     208
     209    /**
     210     * Check, if this response has any extra response data of a specific class
     211     * attached to it.
     212     *
     213     * @param clazz
     214     *            the specific class to check for
     215     * @param <V>
     216     *            the type of {@link SRUExtraResponseData} to check for
     217     *
     218     * @return <code>true</code> if extra response is attached,
     219     *         <code>false</code> otherwise
     220     */
     221    public <V extends SRUExtraResponseData> boolean hasExtraResponseData(
     222            Class<V> clazz) {
     223        if (clazz == null) {
     224            throw new NullPointerException("clazz == null");
     225        }
     226        if (extraResponseData != null) {
     227            for (SRUExtraResponseData i : extraResponseData) {
     228                if (clazz.isInstance(i)) {
     229                    return true;
     230                }
     231            }
     232        }
     233        return false;
     234    }
     235
     236
     237    /**
    149238     * Get the total number of bytes transferred for this request.
    150239     *
Note: See TracChangeset for help on using the changeset viewer.