home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (Edge.java)

This example Java source code file (Edge.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

data, edge, number, override, string

The Edge.java Java example source code

package org.deeplearning4j.models.sequencevectors.graph.primitives;

import lombok.Data;

/** Edge in a graph. May be a directed or undirected edge.<br>
 * Parameterized, and may store a value/object associated with the edge
 */
@Data
public class Edge<T extends Number> {

    private final int from;
    private final int to;
    private final T value;
    private final boolean directed;

    public Edge(int from, int to, T value, boolean directed ){
        this.from = from;
        this.to = to;
        this.value = value;
        this.directed = directed;
    }

    @Override
    public String toString() {
        return "edge(" + (directed ? "directed" : "undirected") + "," + from+  (directed ? "->" : "--") + to + ","
                + (value!=null ? value : "") + ")";
    }

    @Override
    public boolean equals(Object o){
        if(!(o instanceof Edge)) return false;
        Edge<?> e = (Edge)o;
        if(directed != e.directed) return false;
        if(directed){
            if(from != e.from) return false;
            if(to != e.to) return false;
        } else {
            if(from == e.from){
                if(to != e.to) return false;
            } else {
                if(from != e.to) return false;
                if(to != e.from) return false;
            }
        }
        if((value != null && e.value == null) || (value == null && e.value != null)) return false;
        return value == null || value.equals(e.value);
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java Edge.java source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.