1 /*
2  * sys-emu - A system emulator for tutorials
3  * Copyright (C) 2018 - 2019 osdevelopment-info
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18 package info.osdevelopment.sysemu.system
19 
20 import info.osdevelopment.sysemu.model.SystemModel
21 import java.util.UUID
22 import java.util.logging.Logger
23 import scala.collection.mutable
24 
25 /**
26   * A repository with all known systems.
27   */
28 object SystemRepository {
29 
30   def apply() = new SystemRepository
31 
32 }
33 
34 class SystemRepository {
35 
36   private val log = Logger.getLogger(classOf[SystemRepository].getName)
37 
38   /** All known systems. */
39   private val systemMap = mutable.Map[UUID, System]()
40 
41   def create(): Option[SystemModel] = {
42     val system = System()
43     systemMap += (system.uuid -> system)
44     log.info("Created system " + system.uuid)
45     Some(SystemModel(system.uuid))
46   }
47 
48   /**
49     * Remove the system with the given UUID from the list of systems. Returns true if the system was removed, false if
50     * no system with the given UUID was found.
51     * @param uuid the UUID of the system to delete
52     * @return true, if the system existed and was removed
53     */
54   def remove(uuid: UUID): Option[SystemModel] = {
55     log.info("Removing system " + uuid)
56     systemMap remove(uuid) match {
57       case Some(system) => Some(SystemModel(system.uuid))
58       case _ => None
59     }
60   }
61 
62   /**
63     * Returns the system with the given UUID.
64     * @param uuid the UUID of the system to return
65     * @return Some(System) if a system with the given UUID exists or None
66     */
67   def byUUID(uuid: UUID): Option[SystemModel] = {
68     log.fine("Getting system " + uuid)
69     systemMap.get(uuid) match {
70       case Some(system) => Some(SystemModel(system.uuid))
71       case _ => None
72     }
73   }
74 
75   /**
76     * Returns an Iterable with all known systems.
77     * @return all known systems
78     */
79   def all(): Iterable[SystemModel] = {
80     log.fine("Getting all systems")
81     systemMap.values.map(s => SystemModel(s.uuid))
82   }
83 
84 }
Line Stmt Id Pos Tree Symbol Code
30 718 1044 - 1064 Apply info.osdevelopment.sysemu.system.SystemRepository.<init> new SystemRepository()
36 720 1115 - 1166 Apply java.util.logging.Logger.getLogger java.util.logging.Logger.getLogger(classOf[info.osdevelopment.sysemu.system.SystemRepository].getName())
36 719 1132 - 1165 Apply java.lang.Class.getName classOf[info.osdevelopment.sysemu.system.SystemRepository].getName()
39 721 1222 - 1249 Apply scala.collection.MapFactory.Delegate.apply scala.collection.mutable.Map.apply[java.util.UUID, info.osdevelopment.sysemu.system.System]()
42 722 1308 - 1316 Apply info.osdevelopment.sysemu.system.System.apply System.apply()
43 723 1335 - 1356 Apply scala.Predef.ArrowAssoc.-> scala.Predef.ArrowAssoc[java.util.UUID](system.uuid).->[info.osdevelopment.sysemu.system.System](system)
43 724 1321 - 1357 Apply scala.collection.mutable.Growable.+= SystemRepository.this.systemMap.+=(scala.Predef.ArrowAssoc[java.util.UUID](system.uuid).->[info.osdevelopment.sysemu.system.System](system))
44 726 1391 - 1402 Select info.osdevelopment.sysemu.system.System.uuid system.uuid
44 728 1362 - 1403 Apply java.util.logging.Logger.info SystemRepository.this.log.info("Created system ".+(system.uuid))
44 725 1371 - 1388 Literal <nosymbol> "Created system "
44 727 1371 - 1402 Apply java.lang.String.+ "Created system ".+(system.uuid)
45 729 1425 - 1436 Select info.osdevelopment.sysemu.system.System.uuid system.uuid
45 731 1408 - 1438 Apply scala.Some.apply scala.Some.apply[info.osdevelopment.sysemu.model.SystemModel](info.osdevelopment.sysemu.model.SystemModel.apply(system.uuid))
45 730 1413 - 1437 Apply info.osdevelopment.sysemu.model.SystemModel.apply info.osdevelopment.sysemu.model.SystemModel.apply(system.uuid)
55 732 1795 - 1820 Apply java.lang.String.+ "Removing system ".+(uuid)
55 733 1786 - 1821 Apply java.util.logging.Logger.info SystemRepository.this.log.info("Removing system ".+(uuid))
56 734 1826 - 1848 Apply scala.collection.mutable.MapOps.remove SystemRepository.this.systemMap.remove(uuid)
57 735 1901 - 1912 Select info.osdevelopment.sysemu.system.System.uuid system.uuid
57 737 1884 - 1914 Apply scala.Some.apply scala.Some.apply[info.osdevelopment.sysemu.model.SystemModel](info.osdevelopment.sysemu.model.SystemModel.apply(system.uuid))
57 736 1889 - 1913 Apply info.osdevelopment.sysemu.model.SystemModel.apply info.osdevelopment.sysemu.model.SystemModel.apply(system.uuid)
58 738 1931 - 1935 Select scala.None scala.None
68 740 2185 - 2219 Apply java.util.logging.Logger.fine SystemRepository.this.log.fine("Getting system ".+(uuid))
68 739 2194 - 2218 Apply java.lang.String.+ "Getting system ".+(uuid)
69 741 2224 - 2243 Apply scala.collection.MapOps.get SystemRepository.this.systemMap.get(uuid)
70 744 2279 - 2309 Apply scala.Some.apply scala.Some.apply[info.osdevelopment.sysemu.model.SystemModel](info.osdevelopment.sysemu.model.SystemModel.apply(system.uuid))
70 743 2284 - 2308 Apply info.osdevelopment.sysemu.model.SystemModel.apply info.osdevelopment.sysemu.model.SystemModel.apply(system.uuid)
70 742 2296 - 2307 Select info.osdevelopment.sysemu.system.System.uuid system.uuid
71 745 2326 - 2330 Select scala.None scala.None
80 746 2480 - 2511 Apply java.util.logging.Logger.fine SystemRepository.this.log.fine("Getting all systems")
81 747 2554 - 2560 Select info.osdevelopment.sysemu.system.System.uuid s.uuid
81 749 2516 - 2562 Apply scala.collection.IterableOps.map SystemRepository.this.systemMap.values.map[info.osdevelopment.sysemu.model.SystemModel](((s: info.osdevelopment.sysemu.system.System) => info.osdevelopment.sysemu.model.SystemModel.apply(s.uuid)))
81 748 2542 - 2561 Apply info.osdevelopment.sysemu.model.SystemModel.apply info.osdevelopment.sysemu.model.SystemModel.apply(s.uuid)