package act.internal.password;

/*-
 * #%L
 * ACT Framework
 * %%
 * Copyright (C) 2014 - 2018 ActFramework
 * %%
 * 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.
 * #L%
 */

import act.meta.ClassMetaInfoBase;
import act.meta.ClassMetaInfoManager;
import org.osgl.util.E;

import java.util.HashMap;
import java.util.Map;

public class PasswordMetaInfo extends ClassMetaInfoBase<PasswordMetaInfo> {
    // (field name -> field type desc)
    private Map<String, String> passwordFields = new HashMap<>();

    public PasswordMetaInfo addPasswordField(String fieldName, String fieldDesc) {
        passwordFields.put(fieldName, fieldDesc);
        return this;
    }

    public boolean hasPasswordField() {
        return !passwordFields.isEmpty();
    }

    public boolean isPasswordField(String fieldName) {
        return passwordFields.containsKey(fieldName);
    }

    public String passwordFieldTypeDesc(String fieldName) {
        return passwordFields.get(fieldName);
    }

    public boolean isSinglePasswordProvider() {
        return passwordFields.size() == 1;
    }

    public String singlePasswordFieldName() {
        E.illegalStateIf(!isSinglePasswordProvider());
        return passwordFields.keySet().iterator().next();
    }

    public Iterable<String> passwordFieldNames() {
        return passwordFields.keySet();
    }

    public static class Manager extends ClassMetaInfoManager<PasswordMetaInfo> {
    }

}
