public class

Quirks

extends java.lang.Object

 java.lang.Object

↳androidx.camera.core.impl.Quirks

Gradle dependencies

compile group: 'androidx.camera', name: 'camera-core', version: '1.5.0-alpha01'

  • groupId: androidx.camera
  • artifactId: camera-core
  • version: 1.5.0-alpha01

Artifact androidx.camera:camera-core:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

Wraps a list of Quirks, allowing to easily retrieve a Quirk instance by its class.

Summary

Constructors
publicQuirks(java.util.List<Quirk> quirks)

Wraps the provided list of quirks.

Methods
public voidaddQuirkForTesting(Quirk quirk)

Adds an extra quirk.

public booleancontains(java.lang.Class<Quirk> quirkClass)

Returns whether this collection of quirks contains a quirk with the provided type.

public Quirkget(java.lang.Class<Quirk> quirkClass)

Retrieves a Quirk instance given its type.

public java.util.List<Quirk>getAll(java.lang.Class<Quirk> quirkClass)

Retrieves all Quirks of the same or inherited type as the given type.

public static java.lang.StringtoString(Quirks quirks)

Converts a Quirks into a human-readable string representation.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public Quirks(java.util.List<Quirk> quirks)

Wraps the provided list of quirks.

Methods

public Quirk get(java.lang.Class<Quirk> quirkClass)

Retrieves a Quirk instance given its type.

Unlike Quirks.contains(Class), a quirk can only be retrieved by the exact class. If a superclass or superinterface is provided, null will be returned, even if a quirk with the provided superclass or superinterface exists in this collection.

Parameters:

quirkClass: The type of quirk to retrieve.

Returns:

A Quirk instance of the provided type, or null if it isn't found.

public java.util.List<Quirk> getAll(java.lang.Class<Quirk> quirkClass)

Retrieves all Quirks of the same or inherited type as the given type.

Unlike Quirks.get(Class), a quirk can only be retrieved by the exact class. If a superclass or superinterface is provided, all the inherited classes will be returned.

Parameters:

quirkClass: The super type of quirk to retrieve.

Returns:

A Quirk list of the provided type. An empty list is returned if it isn't found.

public boolean contains(java.lang.Class<Quirk> quirkClass)

Returns whether this collection of quirks contains a quirk with the provided type.

This checks whether the provided quirk type is the exact class, a superclass, or a superinterface of any of the contained quirks, and will return true in all cases.

Parameters:

quirkClass: The type of quirk to check for existence in this container.

Returns:

true if this container contains a quirk with the given type, false otherwise.

public void addQuirkForTesting(Quirk quirk)

Adds an extra quirk.

public static java.lang.String toString(Quirks quirks)

Converts a Quirks into a human-readable string representation.

Parameters:

quirks: The Quirks to convert.

Returns:

A pipe-separated string containing the simple class names of each Quirk.

Source

/*
 * Copyright 2020 The Android Open Source Project
 *
 * 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 androidx.camera.core.impl;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;

import java.util.ArrayList;
import java.util.List;

/**
 * Wraps a list of {@link Quirk}s, allowing to easily retrieve a {@link Quirk} instance by its
 * class.
 */
public class Quirks {

    @NonNull
    private final List<Quirk> mQuirks;

    /** Wraps the provided list of quirks. */
    public Quirks(@NonNull final List<Quirk> quirks) {
        mQuirks = new ArrayList<>(quirks);
    }

    /**
     * Retrieves a {@link Quirk} instance given its type.
     *
     * <p>Unlike {@link #contains(Class)}, a quirk can only be retrieved by the exact class. If a
     * superclass or superinterface is provided, {@code null} will be returned, even if a quirk
     * with the provided superclass or superinterface exists in this collection.
     *
     * @param quirkClass The type of quirk to retrieve.
     * @return A {@link Quirk} instance of the provided type, or {@code null} if it isn't found.
     */
    @SuppressWarnings("unchecked")
    @Nullable
    public <T extends Quirk> T get(@NonNull final Class<T> quirkClass) {
        for (final Quirk quirk : mQuirks) {
            if (quirk.getClass() == quirkClass) {
                return (T) quirk;
            }
        }
        return null;
    }

    /**
     * Retrieves all {@link Quirk}s of the same or inherited type as the given type.
     *
     * <p>Unlike {@link #get(Class)}, a quirk can only be retrieved by the exact class. If a
     * superclass or superinterface is provided, all the inherited classes will be returned.
     *
     * @param quirkClass The super type of quirk to retrieve.
     * @return A {@link Quirk} list of the provided type. An empty list is returned if it isn't
     * found.
     */
    @SuppressWarnings("unchecked")
    @NonNull
    public <T extends Quirk> List<T> getAll(@NonNull Class<T> quirkClass) {
        List<T> list = new ArrayList<>();
        for (Quirk quirk : mQuirks) {
            if (quirkClass.isAssignableFrom(quirk.getClass())) {
                list.add((T) quirk);
            }
        }
        return list;
    }

    /**
     * Returns whether this collection of quirks contains a quirk with the provided type.
     *
     * <p>This checks whether the provided quirk type is the exact class, a superclass, or a
     * superinterface of any of the contained quirks, and will return true in all cases.
     * @param quirkClass The type of quirk to check for existence in this container.
     * @return {@code true} if this container contains a quirk with the given type, {@code false}
     * otherwise.
     */
    public boolean contains(@NonNull Class<? extends Quirk> quirkClass) {
        for (Quirk quirk : mQuirks) {
            if (quirkClass.isAssignableFrom(quirk.getClass())) {
                return true;
            }
        }

        return false;
    }

    /** Adds an extra quirk. */
    @VisibleForTesting
    public void addQuirkForTesting(@NonNull Quirk quirk) {
        mQuirks.add(quirk);
    }

    /**
     * Converts a Quirks into a human-readable string representation.
     *
     * @param quirks The Quirks to convert.
     * @return A pipe-separated string containing the simple class names of each Quirk.
     */
    @NonNull
    public static String toString(@NonNull Quirks quirks) {
        List<String> quirkNames = new ArrayList<>();
        for (Quirk quirk : quirks.mQuirks) {
            quirkNames.add(quirk.getClass().getSimpleName());
        }
        return String.join(" | ", quirkNames);
    }
}