/*
 * Copyright (C) 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 com.android.build.config;

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

/**
 * A String and a Position, where it came from in source code.
 */
public class Str implements Comparable<Str> {
    private String mValue;
    private Position mPosition;

    public Str(String s) {
        mValue = s;
        mPosition = new Position();
    }

    public Str(Position pos, String s) {
        mValue = s;
        mPosition = pos;
    }

    public int length() {
        return mValue.length();
    }

    @Override
    public String toString() {
        return mValue;
    }

    public Position getPosition() {
        return mPosition;
    }

    /**
     * Str is equal if the string value is equal, regardless of whether the position
     * is the same.
     */
    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Str)) {
            return false;
        }
        final Str that = (Str)o;
        return mValue.equals(that.mValue);
    }

    @Override
    public int hashCode() {
        return mValue.hashCode();
    }

    @Override
    public int compareTo(Str that) {
        return this.mValue.compareTo(that.mValue);
    }

    public static ArrayList<Str> toList(Position pos, List<String> list) {
        final ArrayList<Str> result = new ArrayList(list.size());
        for (String s: list) {
            result.add(new Str(pos, s));
        }
        return result;
    }
}
