// Copyright 2016 Google, Inc.
//
// 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 com.firebase.jobdispatcher;

import android.support.annotation.IntDef;
import android.support.annotation.VisibleForTesting;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * A Constraint is a runtime requirement for a job. A job only becomes eligible to run once its
 * trigger has been activated and all constraints are satisfied.
 */
public final class Constraint {
    /**
     * Only run the job when an unmetered network is available.
     */
    public static final int ON_UNMETERED_NETWORK = 1;

    /**
     * Only run the job when a network connection is available.
     * If both this and ON_UNMETERED_NETWORK is provided, ON_ANY_NETWORK will take precedence.
     */
    public static final int ON_ANY_NETWORK = 1 << 1;

    /**
     * Only run the job when the device is currently charging.
     */
    public static final int DEVICE_CHARGING = 1 << 2;

    @VisibleForTesting
    static final int[] ALL_CONSTRAINTS = {ON_ANY_NETWORK, ON_UNMETERED_NETWORK, DEVICE_CHARGING};

    /** Constraint shouldn't ever be instantiated. */
    private Constraint() {}

    /**
     * A tooling type-hint for any of the valid constraint values.
     */
    @IntDef(flag = true, value = {
        ON_ANY_NETWORK,
        ON_UNMETERED_NETWORK,
        DEVICE_CHARGING,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface JobConstraint {}

    /**
     * Compact a provided array of constraints into a single int.
     *
     * @see #uncompact(int)
     */
    static int compact(@JobConstraint int[] constraints) {
        int result = 0;
        if (constraints == null) {
            return result;
        }
        for (int c : constraints) {
            result |= c;
        }
        return result;
    }

    /**
     * Unpack a single int into an array of constraints.
     *
     * @see #compact(int[])
     */
    static int[] uncompact(int compactConstraints) {
        int length = 0;
        for (int c : ALL_CONSTRAINTS) {
            length += (compactConstraints & c) == c ? 1 : 0;
        }
        int[] list = new int[length];

        int i = 0;
        for (int c : ALL_CONSTRAINTS) {
            if ((compactConstraints & c) == c) {
                list[i++] = c;
            }
        }

        return list;
    }
}

@@ -1292,23 +1292,16 @@
 ailable.
-%0A     *
  If both
@@ -1309,17 +1309,32 @@
 this and
- 
+%0A     * %7B@link #
 ON_UNMET
@@ -1345,16 +1345,17 @@
 _NETWORK
+%7D
  is prov
@@ -1360,30 +1360,44 @@
 ovided, 
-ON_ANY
+%7B@link #ON_UNMETERED
 _NETWORK
  will ta
@@ -1388,16 +1388,17 @@
 _NETWORK
+%7D
  will ta
@@ -1399,16 +1399,23 @@
 ill take
+%0A     *
  precede

Fix typo in docs. Is what it is supposed to say? Or was this note supposed to be above ON_UNMETERED_NETWORK?