001/* 002 * Trident - A Multithreaded Server Alternative 003 * Copyright 2014 The TridentSDK Team 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package net.tridentsdk.world; 019 020import javax.annotation.concurrent.Immutable; 021import java.io.Serializable; 022import java.util.Objects; 023 024/** 025 * Stores the position (the X and Z coordinates) of a Chunk 026 * 027 * <p>One produces a new ChunkLocation using {@link #create(int, int)}</p> 028 * 029 * <p>You may reuse ChunkLocations, but never modify them. This is so Chunks occupying a ChunkLocation within a world 030 * cannot chnage its position. This comes at a cost of memory, but offers advantages of low overhead thread-safety and 031 * defensive programming.</p> 032 * 033 * @author The TridentSDK Team 034 * @since 0.3-alpha-DP 035 */ 036@Immutable 037public final class ChunkLocation implements Serializable, Cloneable { 038 private static final long serialVersionUID = 9083698035337137603L; 039 private final int x; 040 private final int z; 041 042 private ChunkLocation(int x, int z) { 043 this.x = x; 044 this.z = z; 045 } 046 047 /** 048 * Produces a new chunk coordinate using the two positions specified 049 * 050 * @param x the X coordinate 051 * @param z the Z coordinate 052 * @return the new chunk location 053 */ 054 public static ChunkLocation create(int x, int z) { 055 return new ChunkLocation(x, z); 056 } 057 058 /** 059 * Obtains the X coordinate for the ChunkLocation 060 * 061 * @return the X coordinate 062 */ 063 public int x() { 064 return this.x; 065 } 066 067 /** 068 * Obtains the Z coordinate ofr the ChunkLocation 069 * 070 * @return the Z coordinate 071 */ 072 public int z() { 073 return this.z; 074 } 075 076 @Override 077 public boolean equals(Object obj) { 078 return (obj instanceof ChunkLocation) && 079 ((ChunkLocation) obj).x == x && ((ChunkLocation) obj).z == z; 080 } 081 082 @Override 083 public int hashCode() { 084 return Objects.hash(x, z); 085 } 086 087 @Override 088 public Object clone() { 089 return ChunkLocation.create(x, z); 090 } 091 092 @Override 093 public String toString() { 094 return "ChunkLocation(" + x() + ", " + z() + ")@" + hashCode(); 095 } 096}