Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (C) 2014 SCVNGR, Inc. d/b/a LevelUp
 *
 * 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.
 */

import android.support.annotation.NonNull;

import java.math.BigInteger;

public class Main {
    /**
     * Utility method to check validity of the provided code. The version must be base 36 encoded.
     *
     * @param code the code string to check.
     * @param expectedLength the length the code must be.
     * @param codeVersionStartIndex the start index (INCLUSIVE) to get the code version from.
     * @param codeVersionEndIndex the end index (EXCLUSIVE) to get the code version from.
     * @param expectedVersion the version code that the code's version should be checked against.
     * @return true if the code passes the expected length and version checks.
     */
    public static boolean isValidCode(@NonNull final String code, final int expectedLength,
            final int codeVersionStartIndex, final int codeVersionEndIndex, final int expectedVersion) {
        boolean isValid = true;

        if (code.length() != expectedLength) {
            isValid = false;
        } else {
            final String versionCode = code.substring(codeVersionStartIndex, codeVersionEndIndex);
            final BigInteger i = new BigInteger(versionCode, Character.MAX_RADIX);

            if (i.intValue() != expectedVersion) {
                isValid = false;
            }
        }

        return isValid;
    }
}