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.io; 025 026 import java.io.File; 027 import java.io.IOException; 028 import java.lang.ref.SoftReference; 029 import java.lang.reflect.InvocationTargetException; 030 import java.lang.reflect.Method; 031 032 import org.ujmp.core.Matrix; 033 import org.ujmp.core.MatrixFactory; 034 import org.ujmp.core.enums.FileFormat; 035 import org.ujmp.core.exceptions.MatrixException; 036 import org.ujmp.core.objectmatrix.stub.AbstractObjectMatrix; 037 038 public abstract class LinkMatrix { 039 040 public static Matrix toFile(FileFormat format, File file, Object... parameters) 041 throws MatrixException, IOException { 042 try { 043 Class<?> c = Class.forName("org.ujmp.core.io.LinkMatrix" + format.name()); 044 Method m = c.getMethod("toFile", new Class<?>[] { File.class, Object[].class }); 045 Matrix matrix = (Matrix) m.invoke(null, file, parameters); 046 return matrix; 047 } catch (ClassNotFoundException e) { 048 try { 049 return new DelayedContentMatrix(format, file, parameters); 050 } catch (ClassCastException ex) { 051 throw new MatrixException("format not supported: " + format, e); 052 } 053 } catch (NoSuchMethodException e) { 054 throw new MatrixException("format not supported: " + format, e); 055 } catch (IllegalAccessException e) { 056 throw new MatrixException("format not supported: " + format, e); 057 } catch (InvocationTargetException e) { 058 throw new MatrixException("could not import", e); 059 } 060 } 061 } 062 063 class DelayedContentMatrix extends AbstractObjectMatrix { 064 private static final long serialVersionUID = -2594340094573426876L; 065 066 private SoftReference<Matrix> matrix = null; 067 068 private FileFormat fileformat = null; 069 private File file = null; 070 private Object[] parameters = null; 071 072 public DelayedContentMatrix(FileFormat fileformat, File file, Object... parameters) { 073 this.fileformat = fileformat; 074 this.file = file; 075 this.parameters = parameters; 076 } 077 078 public Object getObject(long... coordinates) throws MatrixException { 079 return getMatrix().getAsObject(coordinates); 080 } 081 082 private Matrix getMatrix() { 083 if (matrix == null || matrix.get() == null) { 084 try { 085 matrix = new SoftReference<Matrix>(MatrixFactory.importFromFile(fileformat, file, 086 parameters)); 087 } catch (Exception e) { 088 return MatrixFactory.emptyMatrix(); 089 } 090 } 091 return matrix.get(); 092 } 093 094 public void setObject(Object value, long... coordinates) { 095 getMatrix().setAsObject(value, coordinates); 096 } 097 098 public boolean contains(long... coordinates) throws MatrixException { 099 return getMatrix().contains(coordinates); 100 } 101 102 public long[] getSize() { 103 return getMatrix().getSize(); 104 } 105 106 public final StorageType getStorageType() { 107 return getMatrix().getStorageType(); 108 } 109 110 }