📄 tapestorage.java
字号:
public int getTotalRewindTime() { return rewindTime_; } /** * Gets the file with the specified name. * The time taken (in seconds) for getting the file can also be * found using {@link gridsim.datagrid.File#getTransactionTime()}. * @param fileName the name of the needed file * @return the file with the specified filename */ public File getFile(String fileName) { File obj = null; // check first whether file name is valid or not if (fileName == null || fileName.length() == 0) { System.out.println(name_ + ".getFile(): Warning - invalid " + "file name."); return obj; } Iterator it = fileList_.iterator(); int size = 0; int index = 0; boolean found = false; // iterate linearly in a tape to find the file while (it.hasNext()) { obj = (File) it.next(); size += obj.getSize(); if (obj.getName().equals(fileName) == true) { found = true; break; } index++; } // if the file is found, then determine the time taken to get it if (found == true) { obj = (File) fileList_.get(index); double rewindTime = getRewindTime(size); double accessTime = getAccessTime(size - obj.getSize()); double transferTime = getTransferTime(obj.getSize()); // total time for this operation obj.setTransactionTime(rewindTime + accessTime + transferTime); } return obj; } /** * Gets the list of file names located on this storage. * * @return a LinkedList of file names */ public List getFileNameList() { return nameList_; } /** * Checks if the file is valid or not. This method checks whether the given * file or the file name of the file is valid. The method name parameter is * used for debugging purposes, to output in which method an error has * occured. * * @param file the file to be checked for validity * @param methodName the name of the method in which we check for * validity of the file * @return <tt>true</tt> if the file is valid, <tt>false</tt> * otherwise */ private boolean isFileValid(File file, String methodName) { // check if the file is invalid or not if (file == null) { System.out.println(name_ + "." + methodName + ": Warning - the given file is null."); return false; } String fileName = file.getName(); if (fileName == null || fileName.length() == 0) { System.out.println(name_ + "." + methodName + ": Warning - invalid file name."); return false; } return true; } /** * Adds a file to the storage. First the method checks if there is enough * space on the storage, then it checks if the file with the same name is * already taken to avoid duplicate filenames. <br> * The time taken (in seconds) for adding the file can also be * found using {@link gridsim.datagrid.File#getTransactionTime()}. * * @param file the file to be added * @return the time taken (in seconds) for adding the specified file */ public double addFile(File file) { double result = 0.0; // check if the file is valid or not if (isFileValid(file, "addFile()") == false) { return result; } // check the capacity if (file.getSize() + currentSize_ >= capacity_) { System.out.println(name_ + ".addFile(): Warning - not enough space" + " to store " + file.getName()); return result; } // check if the same file name is alredy taken if (contains(file.getName()) == false) { double accessTime = getAccessTime(currentSize_); double transferTime = getTransferTime(file.getSize()); nameList_.add(file.getName()); fileList_.add(file); // add the file into the tape currentSize_ += file.getSize(); // increment the current tape size // rewind time is calculated after the file has been written into // the tape. Hence, currentSize = currentSize + file size double rewindTime = getRewindTime(currentSize_); result = accessTime + transferTime + rewindTime; // total time } file.setTransactionTime(result); return result; } /** * Adds a set of files to the storage. * Run through the list of files and save all of them. * The time taken (in seconds) for adding each file can also be * found using {@link gridsim.datagrid.File#getTransactionTime()}. * @param list the files to be added * @return the time taken (in seconds) for adding the specified files */ public double addFile(List list) { double result = 0.0; if (list == null || list.size() == 0) { System.out.println(name_ + ".addFile(): Warning - list is empty."); return result; } Iterator it = list.iterator(); File file = null; // add each file in the list into the tape while (it.hasNext()) { file = (File) it.next(); result += addFile(file); } return result; } /** * Removes a file from the storage -- <b>NOT SUPPORTED</b>.<br> * NOTE: a tape is supposed to be for backup purposes, once it is full, * individual files can't be deleted unless the tape is cleared. * @param fileName the name of the file to be removed * @return <tt>null</tt> */ public File deleteFile(String fileName) { System.out.println(name_ + ".deleteFile(): Not supported."); return null; } /** * Removes a file from the storage -- <b>NOT SUPPORTED</b>.<br> * NOTE: a tape is supposed to be for backup purposes, once it is full, * individual files can't be deleted unless the tape is cleared. * @param fileName the name of the file to be removed * @param file the file which is removed from the storage is returned * through this parameter * @return the time taken (in seconds) for deleting the specified file */ public double deleteFile(String fileName, File file) { return deleteFile(file); } /** * Removes a file from the storage -- <b>NOT SUPPORTED</b>.<br> * NOTE: a tape is supposed to be for backup purposes, once it is full, * individual files can't be deleted unless the tape is cleared. * @param file the file which is removed from the storage is returned * through this parameter * @return the time taken (in seconds) for deleting the specified file */ public double deleteFile(File file) { System.out.println(name_ + ".deleteFile(): Not supported."); return 0.0; } // NOTE: I assume a tape has an indexing system, rather than search through // the whole tape. /** * Checks whether a certain file is on the storage or not. * * @param fileName the name of the file we are looking for * @return <tt>true</tt> if the file is in the storage, <tt>false</tt> * otherwise */ public boolean contains(String fileName) { boolean result = false; if (fileName == null || fileName.length() == 0) { System.out.println(name_ + ".contains(): Warning - invalid file name"); return result; } // check each file in the list Iterator it = nameList_.iterator(); while (it.hasNext()) { String name = (String) it.next(); if (name.equals(fileName) == true) { result = true; break; } } return result; } /** * Checks whether a certain file is on the storage or not. * * @param file the file we are looking for * @return <tt>true</tt> if the file is in the storage, <tt>false</tt> * otherwise */ public boolean contains(File file) { boolean result = false; if (isFileValid(file, "contains()") == false) { return result; } result = contains(file.getName()); return result; } /** * Gets the access time of this tape in seconds. * fileSize should be from the file starting point in the tape. * * @return the access time in seconds. */ private double getAccessTime(double fileSize) { double result = 0.0; if (fileSize > 0 && capacity_ != 0) { result = ((double) fileSize * accessTime_) / capacity_; } return result; } /** * Gets the rewind time in seconds. * fileSize should be from the file ending point in the tape. * * @return the rewind time in seconds */ private double getRewindTime(double fileSize) { double result = 0.0; if (fileSize > 0 && capacity_ != 0) { result = ((double) fileSize * rewindTime_) / capacity_; } return result; } /** * Gets the transfer time in seconds. * fileSize should be from the file ending point in the tape * * @return the transfer time in seconds */ private double getTransferTime(int fileSize) { double result = 0.0; if (fileSize > 0 && capacity_ != 0) { result = ((double) fileSize * maxTransferRate_) / capacity_; } return result; } /** * Renames a file on the storage. * The time taken (in seconds) for renaming the file can also be * found using {@link gridsim.datagrid.File#getTransactionTime()}. * @param file the file we would like to rename * @param newName the new name of the file * * @return <tt>true</tt> if the renaming succeeded, <tt>false</tt> * otherwise */ public boolean renameFile(File file, String newName) { // check whether the new filename is conflict with existing ones // or not boolean result = false; if (contains(newName) == true) { return result; } // replace the file name in the file (physical) list File obj = getFile(file.getName()); if (obj != null) { obj.setName(newName); } else { return result; } // replace the file name in the name list Iterator it = nameList_.iterator(); while (it.hasNext()) { String name = (String) it.next(); if (name.equals(file.getName()) == true) { nameList_.remove(name); nameList_.add(newName); result = true; break; } } return result; }} // end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -