/*
 * Copyright 2016 The original authors.
 *
 *    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 io.sundr.codegen.model;


import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Property extends ModifierSupport {

    private final List<AnnotationRef> annotations;
    private final TypeRef typeRef;
    private final String name;

    public Property(List<AnnotationRef> annotations, TypeRef typeRef, String name, int modifiers, Map<AttributeKey, Object> attributes) {
        super(modifiers, attributes);
        this.annotations = annotations;
        this.typeRef = typeRef;
        this.name = name;
    }

    public List<AnnotationRef> getAnnotations() {
        return annotations;
    }

    public TypeRef getTypeRef() {
        return typeRef;
    }

    public String getName() {
        return name;
    }

    public String getNameCapitalized() {
        StringBuilder sb = new StringBuilder();
        sb.append(name.replaceAll("_", "").substring(0, 1).toUpperCase());
        sb.append(name.replaceAll("_", "").substring(1));
        return sb.toString();
    }

    public Set<ClassRef> getReferences() {
        Set<ClassRef> refs = new LinkedHashSet<ClassRef>();
        if (typeRef instanceof ClassRef) {
            ClassRef classRef = (ClassRef) typeRef;
            refs.addAll(classRef.getReferences());
        }
        for (AnnotationRef a : getAnnotations()) {
            refs.addAll(a.getClassRef().getReferences());
        }

        if (getAttributes().containsKey(ALSO_IMPORT)) {
            Object obj = getAttributes().get(ALSO_IMPORT);
            if (obj instanceof ClassRef) {
                refs.add((ClassRef) obj);
            } else if (obj instanceof Collection) {
                refs.addAll((Collection<? extends ClassRef>) obj);
            }
        }
        return refs;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Property property = (Property) o;

        if (typeRef != null ? !typeRef.equals(property.typeRef) : property.typeRef != null)
            return false;
        return name != null ? name.equals(property.name) : property.name == null;

    }

    @Override
    public int hashCode() {
        int result = typeRef != null ? typeRef.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        if (isPublic()) {
            sb.append(PUBLIC).append(SPACE);
        } else if (isProtected()) {
            sb.append(PROTECTED).append(SPACE);
        } else if (isPrivate()) {
            sb.append(PRIVATE).append(SPACE);
        }

        if (isStatic()) {
            sb.append(STATIC).append(SPACE);
        }

        if (isFinal()) {
            sb.append(FINAL).append(SPACE);
        }

        sb.append(typeRef).append(SPACE);
        sb.append(name);

        return sb.toString();
    }
}

@@ -787,16 +787,84 @@
 til.Set;
+%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.Stream;
 %0A%0Apublic
@@ -1566,165 +1566,167 @@
     
-StringBuilder sb = new StringBuilder();%0A        sb.append(name.replaceAll(%22_%22, %22%22).substring(0, 1).toUpperCase());%0A        sb.append(name.replaceAll(%22_%22, %22%22)
+return Stream.of(name.split(%22%5B%5Ea-zA-Z0-9%5D%22))%0A            .filter(s -%3E s != null && s.length() %3E 0)%0A            .map(v -%3E Character.toUpperCase(v.charAt(0)) + v
 .sub
@@ -1735,17 +1735,16 @@
 ring(1))
-;
 %0A       
@@ -1744,36 +1744,50 @@
         
-return sb.toStr
+    .collect(Collectors.join
 ing()
+)
 ;%0A    %7D%0A

Handle underscores anywhere in property name