public final class

CarColorConstraints

extends java.lang.Object

 java.lang.Object

↳androidx.car.app.model.constraints.CarColorConstraints

Gradle dependencies

compile group: 'androidx.car.app', name: 'app', version: '1.2.0-rc01'

  • groupId: androidx.car.app
  • artifactId: app
  • version: 1.2.0-rc01

Artifact androidx.car.app:app:1.2.0-rc01 it located at Google repository (https://maven.google.com/)

Overview

Encapsulates the constraints to apply when rendering a CarColor on a template.

Summary

Fields
public static final CarColorConstraintsSTANDARD_ONLY

public static final CarColorConstraintsUNCONSTRAINED

Methods
public voidvalidateOrThrow(CarColor carColor)

Returns true if the CarColor meets the constraints' requirement.

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

Fields

public static final CarColorConstraints UNCONSTRAINED

public static final CarColorConstraints STANDARD_ONLY

Methods

public void validateOrThrow(CarColor carColor)

Returns true if the CarColor meets the constraints' requirement.

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.car.app.model.constraints;

import static androidx.annotation.RestrictTo.Scope;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.car.app.model.CarColor;
import androidx.car.app.model.CarColor.CarColorType;

import java.util.HashSet;

/**
 * Encapsulates the constraints to apply when rendering a {@link CarColor} on a template.
 *
 * @hide
 */
@RestrictTo(Scope.LIBRARY)
public final class CarColorConstraints {

    @NonNull
    public static final CarColorConstraints UNCONSTRAINED =
            CarColorConstraints.create(
                    new int[]{
                            CarColor.TYPE_CUSTOM,
                            CarColor.TYPE_DEFAULT,
                            CarColor.TYPE_PRIMARY,
                            CarColor.TYPE_SECONDARY,
                            CarColor.TYPE_RED,
                            CarColor.TYPE_GREEN,
                            CarColor.TYPE_BLUE,
                            CarColor.TYPE_YELLOW
                    });

    @NonNull
    public static final CarColorConstraints STANDARD_ONLY =
            CarColorConstraints.create(
                    new int[]{
                            CarColor.TYPE_DEFAULT,
                            CarColor.TYPE_PRIMARY,
                            CarColor.TYPE_SECONDARY,
                            CarColor.TYPE_RED,
                            CarColor.TYPE_GREEN,
                            CarColor.TYPE_BLUE,
                            CarColor.TYPE_YELLOW
                    });

    @CarColorType
    private final HashSet<Integer> mAllowedTypes;

    private static CarColorConstraints create(int[] allowedColorTypes) {
        return new CarColorConstraints(allowedColorTypes);
    }

    /**
     * Returns {@code true} if the {@link CarColor} meets the constraints' requirement.
     *
     * @throws IllegalArgumentException if the color type is not allowed
     */
    public void validateOrThrow(@NonNull CarColor carColor) {
        @CarColorType int type = carColor.getType();
        if (!mAllowedTypes.contains(type)) {
            throw new IllegalArgumentException("Car color type is not allowed: " + carColor);
        }
    }

    private CarColorConstraints(int[] allowedColorTypes) {
        mAllowedTypes = new HashSet<>();
        for (int type : allowedColorTypes) {
            mAllowedTypes.add(type);
        }
    }
}