Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2007-2011 Hidekatsu Izuno
 *
 * 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.
 */

import java.lang.reflect.Field;

import java.lang.reflect.Modifier;

import java.util.Arrays;

public class Main {
    public static int hashCode(Object target) {
        if (target == null)
            return 0;
        final int prime = 31;
        int result = 1;

        Class<?> current = target.getClass();
        do {
            for (Field f : current.getDeclaredFields()) {
                if (Modifier.isStatic(f.getModifiers()) || Modifier.isTransient(f.getModifiers())
                        || f.isSynthetic()) {
                    continue;
                }

                Object self;
                try {
                    f.setAccessible(true);
                    self = f.get(target);
                } catch (IllegalAccessException e) {
                    throw new IllegalStateException(e);
                }
                if (self == null) {
                    result = prime * result + 0;
                } else if (self.getClass().isArray()) {
                    if (self.getClass().equals(boolean[].class)) {
                        result = prime * result + Arrays.hashCode((boolean[]) self);
                    } else if (self.getClass().equals(char[].class)) {
                        result = prime * result + Arrays.hashCode((char[]) self);
                    } else if (self.getClass().equals(byte[].class)) {
                        result = prime * result + Arrays.hashCode((byte[]) self);
                    } else if (self.getClass().equals(short[].class)) {
                        result = prime * result + Arrays.hashCode((short[]) self);
                    } else if (self.getClass().equals(int[].class)) {
                        result = prime * result + Arrays.hashCode((int[]) self);
                    } else if (self.getClass().equals(long[].class)) {
                        result = prime * result + Arrays.hashCode((long[]) self);
                    } else if (self.getClass().equals(float[].class)) {
                        result = prime * result + Arrays.hashCode((float[]) self);
                    } else if (self.getClass().equals(double[].class)) {
                        result = prime * result + Arrays.hashCode((double[]) self);
                    } else {
                        result = prime * result + Arrays.hashCode((Object[]) self);
                    }
                } else {
                    result = prime * result + self.hashCode();
                }

                System.out.println(f.getName() + ": " + result);
            }
            current = current.getSuperclass();
        } while (!Object.class.equals(current));

        return result;
    }

    public static boolean equals(Object target, Object o) {
        if (target == o)
            return true;
        if (target == null || o == null)
            return false;
        if (!target.getClass().equals(o.getClass()))
            return false;

        Class<?> current = target.getClass();
        do {
            for (Field f : current.getDeclaredFields()) {
                if (Modifier.isStatic(f.getModifiers()) || Modifier.isTransient(f.getModifiers())
                        || f.isSynthetic()) {
                    continue;
                }

                Object self;
                Object other;
                try {
                    f.setAccessible(true);
                    self = f.get(target);
                    other = f.get(o);
                } catch (IllegalAccessException e) {
                    throw new IllegalStateException(e);
                }
                if (self == null) {
                    if (other != null)
                        return false;
                } else if (self.getClass().isArray()) {
                    if (self.getClass().equals(boolean[].class)) {
                        if (!Arrays.equals((boolean[]) self, (boolean[]) other))
                            return false;
                    } else if (self.getClass().equals(char[].class)) {
                        if (!Arrays.equals((char[]) self, (char[]) other))
                            return false;
                    } else if (self.getClass().equals(byte[].class)) {
                        if (!Arrays.equals((byte[]) self, (byte[]) other))
                            return false;
                    } else if (self.getClass().equals(short[].class)) {
                        if (!Arrays.equals((short[]) self, (short[]) other))
                            return false;
                    } else if (self.getClass().equals(int[].class)) {
                        if (!Arrays.equals((int[]) self, (int[]) other))
                            return false;
                    } else if (self.getClass().equals(long[].class)) {
                        if (!Arrays.equals((long[]) self, (long[]) other))
                            return false;
                    } else if (self.getClass().equals(float[].class)) {
                        if (!Arrays.equals((float[]) self, (float[]) other))
                            return false;
                    } else if (self.getClass().equals(double[].class)) {
                        if (!Arrays.equals((double[]) self, (double[]) other))
                            return false;
                    } else {
                        if (!Arrays.equals((Object[]) self, (Object[]) other))
                            return false;
                    }
                } else if (!self.equals(other)) {
                    return false;
                }
            }
            current = current.getSuperclass();
        } while (!Object.class.equals(current));

        return true;
    }
}