/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.index.mapper;


import java.io.IOException;
import java.net.InetAddress;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.InetAddressPoint;
import org.apache.lucene.document.SortedSetDocValuesField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;


/** A {@link FieldMapper} for ip addresses. */
public class IpFieldMapper extends FieldMapper {

    public static final String CONTENT_TYPE = "ip";

    public static class Defaults {
        public static final Explicit<Boolean> IGNORE_MALFORMED = new Explicit<>(false, false);
        public static final FieldType FIELD_TYPE = new FieldType();

        static {
            FIELD_TYPE.setDimensions(1, Integer.BYTES);
            FIELD_TYPE.freeze();
        }
    }

    public static class Builder extends FieldMapper.Builder<Builder> {

        private InetAddress nullValue;

        public Builder(String name) {
            super(name, Defaults.FIELD_TYPE);
            builder = this;
        }

        public Builder nullValue(InetAddress nullValue) {
            this.nullValue = nullValue;
            return builder;
        }

        @Override
        public IpFieldMapper build(BuilderContext context) {
            return new IpFieldMapper(
                name,
                position,
                defaultExpression,
                fieldType,
                new IpFieldType(buildFullName(context), indexed, hasDocValues),
                nullValue,
                context.indexSettings(),
                copyTo);
        }
    }

    public static class TypeParser implements Mapper.TypeParser {

        public TypeParser() {
        }

        @Override
        public Mapper.Builder<?> parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException {
            Builder builder = new Builder(name);
            TypeParsers.parseField(builder, name, node, parserContext);
            for (Iterator<Map.Entry<String, Object>> iterator = node.entrySet().iterator(); iterator.hasNext();) {
                Map.Entry<String, Object> entry = iterator.next();
                String propName = entry.getKey();
                Object propNode = entry.getValue();
                if (propName.equals("null_value")) {
                    if (propNode == null) {
                        throw new MapperParsingException("Property [null_value] cannot be null.");
                    }
                    builder.nullValue(InetAddresses.forString(propNode.toString()));
                    iterator.remove();
                }
            }
            return builder;
        }
    }

    public static final class IpFieldType extends MappedFieldType {

        public IpFieldType(String name, boolean indexed, boolean hasDocValues) {
            super(name, indexed, hasDocValues);
        }

        public IpFieldType(String name) {
            this(name, true, true);
        }

        @Override
        public String typeName() {
            return CONTENT_TYPE;
        }
    }

    private final InetAddress nullValue;

    private IpFieldMapper(
            String simpleName,
            Integer position,
            String defaultExpression,
            FieldType fieldType,
            MappedFieldType mappedFieldType,
            InetAddress nullValue,
            Settings indexSettings,
            CopyTo copyTo) {
        super(simpleName, position, defaultExpression, fieldType, mappedFieldType, indexSettings, copyTo);
        this.nullValue = nullValue;
    }

    @Override
    public IpFieldType fieldType() {
        return (IpFieldType) super.fieldType();
    }

    @Override
    protected String contentType() {
        return fieldType().typeName();
    }

    @Override
    protected IpFieldMapper clone() {
        return (IpFieldMapper) super.clone();
    }

    @Override
    protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
        Object addressAsObject = context.parser().textOrNull();

        if (addressAsObject == null) {
            addressAsObject = nullValue;;
        }

        if (addressAsObject == null) {
            return;
        }

        String addressAsString = addressAsObject.toString();
        InetAddress address;
        if (addressAsObject instanceof InetAddress) {
            address = (InetAddress) addressAsObject;
        } else {
            address = InetAddresses.forString(addressAsString);
        }

        if (fieldType().isSearchable()) {
            fields.add(new InetAddressPoint(fieldType().name(), address));
        }
        if (fieldType().hasDocValues()) {
            fields.add(new SortedSetDocValuesField(fieldType().name(), new BytesRef(InetAddressPoint.encode(address))));
        } else if (fieldType.stored() || fieldType().isSearchable()) {
            createFieldNamesField(context, fields);
        }
        if (fieldType.stored()) {
            fields.add(new StoredField(fieldType().name(), new BytesRef(InetAddressPoint.encode(address))));
        }
    }

    @Override
    protected void mergeOptions(FieldMapper other, List<String> conflicts) {
        IpFieldMapper mergeWith = (IpFieldMapper) other;
        if (mergeWith.nullValue != this.nullValue) {
            conflicts.add("mapper [" + name() + "] has different [null_value] values");
        }
    }

    @Override
    protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException {
        super.doXContentBody(builder, includeDefaults, params);

        if (nullValue != null) {
            builder.field("null_value", InetAddresses.toAddrString(nullValue));
        }
    }
}

@@ -886,35 +886,8 @@
 ss;%0A
-import java.util.Iterator;%0A
 impo
@@ -905,16 +905,16 @@
 l.List;%0A
+
 import j
@@ -1370,66 +1370,8 @@
 gs;%0A
-import org.elasticsearch.common.xcontent.XContentBuilder;%0A
 %0A%0A/*
@@ -1916,48 +1916,8 @@
  %7B%0A%0A
-        private InetAddress nullValue;%0A%0A
     
@@ -2039,145 +2039,8 @@
  %7D%0A%0A
-        public Builder nullValue(InetAddress nullValue) %7B%0A            this.nullValue = nullValue;%0A            return builder;%0A        %7D%0A%0A
     
@@ -2346,35 +2346,8 @@
 s),%0A
-                nullValue,%0A
     
@@ -2815,666 +2815,8 @@
 t);%0A
-            for (Iterator%3CMap.Entry%3CString, Object%3E%3E iterator = node.entrySet().iterator(); iterator.hasNext();) %7B%0A                Map.Entry%3CString, Object%3E entry = iterator.next();%0A                String propName = entry.getKey();%0A                Object propNode = entry.getValue();%0A                if (propName.equals(%22null_value%22)) %7B%0A                    if (propNode == null) %7B%0A                        throw new MapperParsingException(%22Property %5Bnull_value%5D cannot be null.%22);%0A                    %7D%0A                    builder.nullValue(InetAddresses.forString(propNode.toString()));%0A                    iterator.remove();%0A                %7D%0A            %7D%0A
     
@@ -3261,50 +3261,8 @@
  %7D%0A%0A
-    private final InetAddress nullValue;%0A%0A
     
@@ -3465,43 +3465,8 @@
 pe,%0A
-            InetAddress nullValue,%0A
     
@@ -3637,44 +3637,8 @@
 o);%0A
-        this.nullValue = nullValue;%0A
     
@@ -4139,100 +4139,8 @@
 );%0A%0A
-        if (addressAsObject == null) %7B%0A            addressAsObject = nullValue;;%0A        %7D%0A%0A
     
@@ -5083,32 +5083,32 @@
 %0A%0A    @Override%0A
+
     protected vo
@@ -5172,545 +5172,8 @@
 ) %7B%0A
-        IpFieldMapper mergeWith = (IpFieldMapper) other;%0A        if (mergeWith.nullValue != this.nullValue) %7B%0A            conflicts.add(%22mapper %5B%22 + name() + %22%5D has different %5Bnull_value%5D values%22);%0A        %7D%0A    %7D%0A%0A    @Override%0A    protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException %7B%0A        super.doXContentBody(builder, includeDefaults, params);%0A%0A        if (nullValue != null) %7B%0A            builder.field(%22null_value%22, InetAddresses.toAddrString(nullValue));%0A        %7D%0A
     

Remove unused null_value from IpFieldMapper