001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.core.objectmatrix.stub; 025 026 import java.util.Map; 027 028 import org.ujmp.core.Coordinates; 029 import org.ujmp.core.Matrix; 030 import org.ujmp.core.exceptions.MatrixException; 031 import org.ujmp.core.interfaces.Wrapper; 032 import org.ujmp.core.objectmatrix.ObjectMatrix2D; 033 import org.ujmp.core.objectmatrix.impl.DefaultDenseObjectMatrix2D; 034 import org.ujmp.core.util.CoordinateIterator2D; 035 import org.ujmp.core.util.MathUtil; 036 037 public abstract class AbstractMapToTiledMatrix2DWrapper extends AbstractDenseObjectMatrix2D 038 implements ObjectMatrix2D, Wrapper<Map<Coordinates, ObjectMatrix2D>> { 039 040 private static final long serialVersionUID = -7464578359102479614L; 041 042 private final long[] tileSize = new long[] { 50, 50 }; 043 044 private long[] size = null; 045 046 private final Map<Coordinates, ObjectMatrix2D> values; 047 048 public AbstractMapToTiledMatrix2DWrapper(Map<Coordinates, ObjectMatrix2D> map, long... size) { 049 this.values = map; 050 this.size = Coordinates.copyOf(size); 051 } 052 053 public AbstractMapToTiledMatrix2DWrapper(Map<Coordinates, ObjectMatrix2D> map, Matrix source) { 054 this(map, source.getSize()); 055 for (long[] c : source.availableCoordinates()) { 056 setObject(source.getAsObject(c), c); 057 if (!MathUtil.equals(getObject(c), source.getAsObject(c))) { 058 throw new MatrixException("error"); 059 } 060 } 061 } 062 063 public synchronized Object getObject(int row, int column) throws MatrixException { 064 return getObject((long) row, (long) column); 065 } 066 067 public final Map<Coordinates, ObjectMatrix2D> getMap() { 068 return values; 069 } 070 071 public synchronized Object getObject(long row, long column) throws MatrixException { 072 Coordinates c = new Coordinates(row / tileSize[ROW], column / tileSize[COLUMN]); 073 Matrix m = getMap().get(c); 074 if (m == null) { 075 return null; 076 } else { 077 return m.getAsObject(row % tileSize[ROW], column % tileSize[COLUMN]); 078 } 079 } 080 081 public final Map<Coordinates, ObjectMatrix2D> getWrappedObject() { 082 return getMap(); 083 } 084 085 public final void setWrappedObject(Map<Coordinates, ObjectMatrix2D> object) { 086 throw new MatrixException("cannot change map"); 087 } 088 089 public Iterable<long[]> allCoordinates() { 090 return new CoordinateIterator2D(getSize()); 091 } 092 093 public synchronized final double getAsDouble(long... coordinates) throws MatrixException { 094 return MathUtil.getDouble(getObject(coordinates)); 095 } 096 097 public synchronized final void setAsDouble(double v, long... coordinates) 098 throws MatrixException { 099 setObject(v, coordinates); 100 } 101 102 public synchronized void setObject(Object o, int row, int column) throws MatrixException { 103 setObject(o, (long) row, (long) column); 104 } 105 106 public synchronized void setObject(Object o, long row, long column) throws MatrixException { 107 Coordinates c = new Coordinates(row / tileSize[ROW], column / tileSize[COLUMN]); 108 ObjectMatrix2D m = getMap().get(c); 109 if (m == null) { 110 m = new DefaultDenseObjectMatrix2D(tileSize[ROW], tileSize[COLUMN]); 111 getMap().put(c, m); 112 } 113 m.setObject(o, row % tileSize[ROW], column % tileSize[COLUMN]); 114 } 115 116 public final long[] getSize() { 117 return size; 118 } 119 120 public final long[] getTileSize() { 121 return tileSize; 122 } 123 124 }