001/* 002 * To change this template, choose Tools | Templates 003 * and open the template in the editor. 004 */ 005package org.anarres.qemu.exec; 006 007import java.io.File; 008import java.util.List; 009import javax.annotation.Nonnegative; 010import javax.annotation.Nonnull; 011 012/** 013 * 014 * @author shevek 015 */ 016public class QEmuMemoryOption extends AbstractQEmuOption { 017 018 // TODO -> Generic package where it can be used for file sizes, etc. 019 public static enum Magnitude { 020 021 UNIT(1L), 022 KILO(1024L), 023 MEGA(KILO, 1024L), 024 GIGA(MEGA, 1024L), 025 TERA(GIGA, 1024L), 026 PETA(TERA, 1024L); 027 private final long multiplier; 028 029 Magnitude(@Nonnegative long multiplier) { 030 this.multiplier = multiplier; 031 } 032 033 Magnitude(@Nonnull Magnitude reference, @Nonnegative long multiplier) { 034 this.multiplier = reference.multiplier * multiplier; 035 } 036 037 public long toUnit(long amount) { 038 return multiplier * amount; 039 } 040 } 041 // private final String host; 042 private final long size; 043 private File path; 044 private boolean prealloc; 045 046 public QEmuMemoryOption(long size, @Nonnull Magnitude magnitude) { 047 this.size = magnitude.toUnit(size) / Magnitude.MEGA.multiplier; 048 } 049 050 @Nonnull 051 public QEmuMemoryOption withPath(File path) { 052 this.path = path; 053 return this; 054 } 055 056 @Nonnull 057 public QEmuMemoryOption withPrealloc(boolean prealloc) { 058 this.prealloc = prealloc; 059 return this; 060 } 061 062 @Override 063 public void appendTo(List<? super String> line) { 064 add(line, "-m", String.valueOf(size)); 065 if (path != null) 066 add(line, "-mem-path", path.getAbsolutePath()); 067 if (prealloc) 068 add(line, "-mem-prealloc"); 069 } 070}