// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage.expressions;

import com.yahoo.document.DataType;
import com.yahoo.document.DocumentType;
import com.yahoo.document.datatypes.StringFieldValue;

import java.net.InetAddress;

/**
 * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
 */
public class HostNameExpression extends Expression {

    @Override
    protected void doExecute(ExecutionContext ctx) {
        try {
            ctx.setValue(new StringFieldValue(normalizeHostName(InetAddress.getLocalHost().getHostName())));
        } catch (java.net.UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doVerify(VerificationContext context) {
        context.setValue(createdOutputType());
    }

    @Override
    public DataType requiredInputType() {
        return null;
    }

    @Override
    public DataType createdOutputType() {
        return DataType.STRING;
    }

    @Override
    public String toString() {
        return "hostname";
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof HostNameExpression)) {
            return false;
        }
        return true;
    }

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

    public static String normalizeHostName(String hostName) {
        int pos = hostName.indexOf('.');
        return pos < 0 ? hostName : hostName.substring(0, pos);
    }
}

@@ -301,36 +301,37 @@
 ue;%0A
-%0A
 import 
-java.net.InetAddress
+com.yahoo.net.HostName
 ;%0A%0A/
@@ -529,26 +529,8 @@
 ) %7B%0A
-        try %7B%0A    
     
@@ -589,19 +589,16 @@
 ame(
-InetAddress
+HostName
 .get
@@ -606,137 +606,18 @@
 ocal
-H
+h
 ost()
-.getHostName())));%0A        %7D catch (java.net.UnknownHostException e) %7B%0A            throw new RuntimeException(e);%0A        %7D
+)));
 %0A   

use HostName.getLocalhost