All Downloads are FREE. Search and download functionalities are using the official Maven repository.

swim.dynamic.java.lang.HostObject Maven / Gradle / Ivy

There is a newer version: 4.3.15
Show newest version
// Copyright 2015-2023 Nstream, inc.
//
// 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 swim.dynamic.java.lang;

import swim.dynamic.Bridge;
import swim.dynamic.HostMethod;
import swim.dynamic.HostObjectType;
import swim.dynamic.JavaHostBuiltinType;

public final class HostObject {

  private HostObject() {
    // static
  }

  public static final HostObjectType TYPE;

  static {
    final JavaHostBuiltinType type = new JavaHostBuiltinType<>(Object.class);
    TYPE = type;
    type.addMember(new HostObjectEquals());
    type.addMember(new HostObjectHashCode());
    type.addMember(new HostObjectToString());
  }

}

final class HostObjectEquals implements HostMethod {

  @Override
  public String key() {
    return "equals";
  }

  @Override
  public Object invoke(Bridge bridge, Object object, Object... arguments) {
    return object.equals(arguments[0]);
  }

}

final class HostObjectHashCode implements HostMethod {

  @Override
  public String key() {
    return "hashCode";
  }

  @Override
  public Object invoke(Bridge bridge, Object object, Object... arguments) {
    return object.hashCode();
  }

}

final class HostObjectToString implements HostMethod {

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

  @Override
  public Object invoke(Bridge bridge, Object object, Object... arguments) {
    if (object != null) {
      return object.toString();
    } else {
      return "null";
    }
  }

}