├── .gitignore ├── LICENCE ├── README.md ├── src └── com │ └── oocourse │ ├── TimableOutput.java │ ├── elevator2 │ ├── ElevatorInput.java │ └── PersonRequest.java │ ├── elevator3 │ ├── ElevatorInput.java │ ├── PersonRequest.java │ └── exception │ │ ├── DuplicatedFromToFloorException.java │ │ ├── DuplicatedPersonIdException.java │ │ ├── InvalidFromFloorException.java │ │ ├── InvalidPatternException.java │ │ ├── InvalidPersonIdException.java │ │ ├── InvalidToFloorException.java │ │ └── PersonRequestException.java │ └── timed │ ├── TimedInputPair.java │ ├── TimedMiddleware.java │ └── TimedWriter.java └── test └── com └── oocourse ├── elevator2 └── ElevatorInputTest.java └── elevator3 └── ElevatorInputTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | origin_jar/ 2 | 3 | 4 | # Created by https://www.gitignore.io/api/intellij+all 5 | # Edit at https://www.gitignore.io/?templates=intellij+all 6 | 7 | ### Intellij+all ### 8 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 9 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 10 | 11 | # User-specific stuff 12 | .idea/**/workspace.xml 13 | .idea/**/tasks.xml 14 | .idea/**/usage.statistics.xml 15 | .idea/**/dictionaries 16 | .idea/**/shelf 17 | 18 | # Generated files 19 | .idea/**/contentModel.xml 20 | 21 | # Sensitive or high-churn files 22 | .idea/**/dataSources/ 23 | .idea/**/dataSources.ids 24 | .idea/**/dataSources.local.xml 25 | .idea/**/sqlDataSources.xml 26 | .idea/**/dynamic.xml 27 | .idea/**/uiDesigner.xml 28 | .idea/**/dbnavigator.xml 29 | 30 | # Gradle 31 | .idea/**/gradle.xml 32 | .idea/**/libraries 33 | 34 | # Gradle and Maven with auto-import 35 | # When using Gradle or Maven with auto-import, you should exclude module files, 36 | # since they will be recreated, and may cause churn. Uncomment if using 37 | # auto-import. 38 | # .idea/modules.xml 39 | # .idea/*.iml 40 | # .idea/modules 41 | 42 | # CMake 43 | cmake-build-*/ 44 | 45 | # Mongo Explorer plugin 46 | .idea/**/mongoSettings.xml 47 | 48 | # File-based project format 49 | *.iws 50 | 51 | # IntelliJ 52 | out/ 53 | 54 | # mpeltonen/sbt-idea plugin 55 | .idea_modules/ 56 | 57 | # JIRA plugin 58 | atlassian-ide-plugin.xml 59 | 60 | # Cursive Clojure plugin 61 | .idea/replstate.xml 62 | 63 | # Crashlytics plugin (for Android Studio and IntelliJ) 64 | com_crashlytics_export_strings.xml 65 | crashlytics.properties 66 | crashlytics-build.properties 67 | fabric.properties 68 | 69 | # Editor-based Rest Client 70 | .idea/httpRequests 71 | 72 | # Android studio 3.1+ serialized cache file 73 | .idea/caches/build_file_checksums.ser 74 | 75 | # JetBrains templates 76 | **___jb_tmp___ 77 | 78 | ### Intellij+all Patch ### 79 | # Ignores the whole .idea folder and all .iml files 80 | # See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 81 | 82 | .idea/ 83 | 84 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 85 | 86 | *.iml 87 | modules.xml 88 | .idea/misc.xml 89 | *.ipr 90 | 91 | # Sonarlint plugin 92 | .idea/sonarlint 93 | 94 | # End of https://www.gitignore.io/api/intellij+all 95 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BUAAOO 电梯单元测试IO接口 2 | 3 | 为电梯单元自动化黑箱测试搭建的IO接口 4 | 5 | 与官方提供的程序接口格式一致,可以直接替换使用 6 | 7 | ## Getting Started 8 | 9 | 将编译好的JAR包替换课程组提供的两个包 10 | 11 | 输入格式变为带有时间的输入: 12 | 13 | ``` 14 | [0.0]1-FROM-1-TO-2 15 | [1.0]2-FROM-1-TO-2 16 | [2.0]3-FROM-1-TO-2 17 | ``` 18 | 全部输入完毕后手动^D给出EOF,若格式无误会自动按给定的时间顺序输入电梯 19 | 20 | ## Contributing 21 | 22 | 欢迎各种形式的良性Issue与PR 23 | 24 | ## License 25 | 26 | GPL 2.0 -------------------------------------------------------------------------------- /src/com/oocourse/TimableOutput.java: -------------------------------------------------------------------------------- 1 | package com.oocourse; 2 | 3 | 4 | import java.io.PrintStream; 5 | 6 | public abstract class TimableOutput { 7 | private static final double SECOND_TO_MILLISECOND = 1000.0D; 8 | private static final PrintStream DEFAULT_PRINT_STREAM; 9 | private static long startTimestamp = 0L; 10 | 11 | static { 12 | DEFAULT_PRINT_STREAM = System.out; 13 | } 14 | 15 | public TimableOutput() { 16 | } 17 | 18 | public static boolean initStartTimestamp() { 19 | startTimestamp = System.currentTimeMillis(); 20 | return true; 21 | } 22 | 23 | private static long getRelativeTimestamp(long timestamp) { 24 | return timestamp - startTimestamp; 25 | } 26 | 27 | private static long getRelativeTimestamp() { 28 | return getRelativeTimestamp(System.currentTimeMillis()); 29 | } 30 | 31 | public static long println(Object obj, PrintStream stream) { 32 | TimableOutput.ObjectWithTimestamp value = 33 | new TimableOutput.ObjectWithTimestamp(obj); 34 | stream.println(value.toEncryptedString()); 35 | return value.getTimestamp(); 36 | } 37 | 38 | public static long println(Object obj) { 39 | return println(obj, DEFAULT_PRINT_STREAM); 40 | } 41 | 42 | public static long println(int i, PrintStream stream) { 43 | return println((Object) String.valueOf(i), stream); 44 | } 45 | 46 | public static long println(int i) { 47 | return println(i, DEFAULT_PRINT_STREAM); 48 | } 49 | 50 | public static long println(boolean b, PrintStream stream) { 51 | return println((Object) String.valueOf(b), stream); 52 | } 53 | 54 | public static long println(boolean b) { 55 | return println(b, DEFAULT_PRINT_STREAM); 56 | } 57 | 58 | public static long println(char c, PrintStream stream) { 59 | return println((Object) String.valueOf(c), stream); 60 | } 61 | 62 | public static long println(char c) { 63 | return println(c, DEFAULT_PRINT_STREAM); 64 | } 65 | 66 | public static long println(long l, PrintStream stream) { 67 | return println((Object) String.valueOf(l), stream); 68 | } 69 | 70 | public static long println(long l) { 71 | return println(l, DEFAULT_PRINT_STREAM); 72 | } 73 | 74 | public static long println(float f, PrintStream stream) { 75 | return println((Object) String.valueOf(f), stream); 76 | } 77 | 78 | public static long println(float f) { 79 | return println(f, DEFAULT_PRINT_STREAM); 80 | } 81 | 82 | public static long println(char[] s, PrintStream stream) { 83 | return println((Object) String.valueOf(s), stream); 84 | } 85 | 86 | public static long println(char[] s) { 87 | return println(s, DEFAULT_PRINT_STREAM); 88 | } 89 | 90 | public static long println(double d, PrintStream stream) { 91 | return println((Object) String.valueOf(d), stream); 92 | } 93 | 94 | public static long println(double d) { 95 | return println(d, DEFAULT_PRINT_STREAM); 96 | } 97 | 98 | private static class ObjectWithTimestamp { 99 | private final long timestamp; 100 | private final Object object; 101 | 102 | ObjectWithTimestamp(long timestamp, Object object) { 103 | this.timestamp = timestamp; 104 | this.object = object; 105 | } 106 | 107 | ObjectWithTimestamp(Object object) { 108 | this(System.currentTimeMillis(), object); 109 | } 110 | 111 | public long getTimestamp() { 112 | return this.timestamp; 113 | } 114 | 115 | public long getRelativeTimestamp() { 116 | return TimableOutput.getRelativeTimestamp(this.getTimestamp()); 117 | } 118 | 119 | public double getRelativeSecondTimestamp() { 120 | return (double) this.getRelativeTimestamp() / 1000.0D; 121 | } 122 | 123 | public Object getObject() { 124 | return this.object; 125 | } 126 | 127 | @Override 128 | public String toString() { 129 | return String.format("[%9.4f]%s", 130 | this.getRelativeSecondTimestamp(), this.getObject().toString()); 131 | } 132 | 133 | public String toEncryptedString() { 134 | return this.toString(); 135 | } 136 | } 137 | } 138 | 139 | -------------------------------------------------------------------------------- /src/com/oocourse/elevator2/ElevatorInput.java: -------------------------------------------------------------------------------- 1 | package com.oocourse.elevator2; 2 | 3 | import com.oocourse.timed.TimedMiddleware; 4 | 5 | import java.io.Closeable; 6 | import java.io.IOException; 7 | import java.io.InputStream; 8 | import java.util.HashSet; 9 | import java.util.Scanner; 10 | 11 | public class ElevatorInput implements Closeable { 12 | private static final InputStream DEFAULT_INPUT_STREAM; 13 | 14 | static { 15 | DEFAULT_INPUT_STREAM = System.in; 16 | } 17 | 18 | private final Scanner scanner; 19 | private final HashSet existedPersonId; 20 | 21 | public ElevatorInput(InputStream inputStream) { 22 | TimedMiddleware middleware = new TimedMiddleware(inputStream); 23 | this.scanner = new Scanner(middleware.getParsedInputStream()); 24 | this.existedPersonId = new HashSet(); 25 | } 26 | 27 | public ElevatorInput() { 28 | this(DEFAULT_INPUT_STREAM); 29 | } 30 | 31 | @Override 32 | public void close() throws IOException { 33 | this.scanner.close(); 34 | } 35 | 36 | public PersonRequest nextPersonRequest() { 37 | while (this.scanner.hasNextLine()) { 38 | String line = this.scanner.nextLine(); 39 | try { 40 | PersonRequest request = PersonRequest.parse(line); 41 | if (this.existedPersonId.contains(request.getPersonId())) { 42 | throw new Error(line); 43 | } 44 | 45 | this.existedPersonId.add(request.getPersonId()); 46 | return request; 47 | } catch (Error var3) { 48 | var3.printStackTrace(System.err); 49 | } 50 | } 51 | 52 | return null; 53 | } 54 | } 55 | 56 | -------------------------------------------------------------------------------- /src/com/oocourse/elevator2/PersonRequest.java: -------------------------------------------------------------------------------- 1 | package com.oocourse.elevator2; 2 | 3 | import java.math.BigInteger; 4 | import java.util.Arrays; 5 | import java.util.regex.Matcher; 6 | import java.util.regex.Pattern; 7 | 8 | public class PersonRequest { 9 | private static final int MAX_FLOOR = 16; 10 | private static final int ZERO_FLOOR = 0; 11 | private static final int MIN_FLOOR = -3; 12 | private static final String PARSE_PATTERN_STRING = "^(?\\d+)" + 13 | "-FROM-(?(-|\\+|)\\d+)-TO-(?(-|\\+|)\\d+)\\s*$"; 14 | private static final Pattern PARSE_PATTERN = Pattern.compile("^" + 15 | "(?\\d+)-FROM-(?(-|\\+|)\\d+)-TO-(?" + 16 | "(-|\\+|)\\d+)\\s*$"); 17 | private static final BigInteger INT_MAX = BigInteger.valueOf(2147483647L); 18 | private static final BigInteger INT_MIN = BigInteger.valueOf(-2147483648L); 19 | private final int fromFloor; 20 | private final int toFloor; 21 | private final int personId; 22 | 23 | private PersonRequest(int fromFloor, int toFloor, int personId) { 24 | this.fromFloor = fromFloor; 25 | this.toFloor = toFloor; 26 | this.personId = personId; 27 | } 28 | 29 | private static boolean isValidFloor(Integer floor) { 30 | return floor != null && floor != 0 && floor >= -3 && floor <= 16; 31 | } 32 | 33 | private static boolean isValidInteger(String string) { 34 | try { 35 | BigInteger integer = new BigInteger(string); 36 | return integer.compareTo(INT_MAX) <= 0 && integer.compareTo(INT_MIN) >= 0; 37 | } catch (Exception var2) { 38 | return false; 39 | } 40 | } 41 | 42 | private static Integer toValidInteger(String string) { 43 | return isValidInteger(string) ? (new BigInteger(string)).intValue() : 44 | null; 45 | } 46 | 47 | static PersonRequest parse(String string) throws Error { 48 | Matcher matcher = PARSE_PATTERN.matcher(string); 49 | if (matcher.matches()) { 50 | String personIdString = matcher.group("personId"); 51 | Integer personId = toValidInteger(personIdString); 52 | if (personId == null) { 53 | throw new Error(string); 54 | } else { 55 | String fromFloorString = matcher.group("fromFloor"); 56 | Integer fromFloor = toValidInteger(fromFloorString); 57 | if (!isValidFloor(fromFloor)) { 58 | throw new Error(string); 59 | } else { 60 | String toFloorString = matcher.group("toFloor"); 61 | Integer toFloor = toValidInteger(toFloorString); 62 | if (!isValidFloor(toFloor)) { 63 | throw new Error(string); 64 | } else if (fromFloor == toFloor) { 65 | throw new Error(string); 66 | } else { 67 | return new PersonRequest(fromFloor, toFloor, personId); 68 | } 69 | } 70 | } 71 | } else { 72 | throw new Error(string); 73 | } 74 | } 75 | 76 | public int getFromFloor() { 77 | return this.fromFloor; 78 | } 79 | 80 | public int getToFloor() { 81 | return this.toFloor; 82 | } 83 | 84 | public int getPersonId() { 85 | return this.personId; 86 | } 87 | 88 | @Override 89 | public String toString() { 90 | return String.format("%d-FROM-%d-TO-%d", this.personId, 91 | this.fromFloor, this.toFloor); 92 | } 93 | 94 | @Override 95 | public int hashCode() { 96 | return Arrays.hashCode(new int[]{this.personId, this.fromFloor, 97 | this.toFloor}); 98 | } 99 | 100 | @Override 101 | public boolean equals(Object obj) { 102 | if (obj == this) { 103 | return true; 104 | } else if (!(obj instanceof PersonRequest)) { 105 | return false; 106 | } else { 107 | return ((PersonRequest) obj).fromFloor == this.fromFloor && ((PersonRequest) obj).toFloor == this.toFloor && ((PersonRequest) obj).personId == this.personId; 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/com/oocourse/elevator3/ElevatorInput.java: -------------------------------------------------------------------------------- 1 | // 2 | // Source code recreated from a .class file by IntelliJ IDEA 3 | // (powered by Fernflower decompiler) 4 | // 5 | 6 | package com.oocourse.elevator3; 7 | 8 | import com.oocourse.elevator3.exception.DuplicatedPersonIdException; 9 | import com.oocourse.elevator3.exception.PersonRequestException; 10 | import com.oocourse.timed.TimedMiddleware; 11 | 12 | import java.io.Closeable; 13 | import java.io.IOException; 14 | import java.io.InputStream; 15 | import java.util.HashSet; 16 | import java.util.Scanner; 17 | 18 | public class ElevatorInput implements Closeable { 19 | private static final InputStream DEFAULT_INPUT_STREAM; 20 | 21 | static { 22 | DEFAULT_INPUT_STREAM = System.in; 23 | } 24 | 25 | private final Scanner scanner; 26 | private final HashSet existedPersonId; 27 | 28 | public ElevatorInput(InputStream inputStream) { 29 | TimedMiddleware middleware = new TimedMiddleware(inputStream); 30 | this.scanner = new Scanner(middleware.getParsedInputStream()); 31 | this.existedPersonId = new HashSet(); 32 | } 33 | 34 | public ElevatorInput() { 35 | this(DEFAULT_INPUT_STREAM); 36 | } 37 | 38 | public void close() throws IOException { 39 | this.scanner.close(); 40 | } 41 | 42 | public PersonRequest nextPersonRequest() { 43 | while (this.scanner.hasNextLine()) { 44 | String line = this.scanner.nextLine(); 45 | 46 | try { 47 | PersonRequest request = PersonRequest.parse(line); 48 | if (this.existedPersonId.contains(request.getPersonId())) { 49 | throw new DuplicatedPersonIdException(line); 50 | } 51 | 52 | this.existedPersonId.add(request.getPersonId()); 53 | return request; 54 | } catch (PersonRequestException var3) { 55 | var3.printStackTrace(System.err); 56 | } 57 | } 58 | 59 | return null; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/com/oocourse/elevator3/PersonRequest.java: -------------------------------------------------------------------------------- 1 | // 2 | // Source code recreated from a .class file by IntelliJ IDEA 3 | // (powered by Fernflower decompiler) 4 | // 5 | 6 | package com.oocourse.elevator3; 7 | 8 | import com.oocourse.elevator3.exception.DuplicatedFromToFloorException; 9 | import com.oocourse.elevator3.exception.InvalidFromFloorException; 10 | import com.oocourse.elevator3.exception.InvalidPatternException; 11 | import com.oocourse.elevator3.exception.InvalidPersonIdException; 12 | import com.oocourse.elevator3.exception.InvalidToFloorException; 13 | import com.oocourse.elevator3.exception.PersonRequestException; 14 | 15 | import java.math.BigInteger; 16 | import java.util.Arrays; 17 | import java.util.regex.Matcher; 18 | import java.util.regex.Pattern; 19 | 20 | public class PersonRequest { 21 | private static final int MAX_FLOOR = 20; 22 | private static final int ZERO_FLOOR = 0; 23 | private static final int MIN_FLOOR = -3; 24 | private static final String PARSE_PATTERN_STRING 25 | = "^(?\\d+)-FROM-(?(-|\\+|)\\d+)-TO-(?" + 26 | "(-|\\+|)\\d+)\\s*$"; 27 | private static final Pattern PARSE_PATTERN = Pattern.compile("^" + 28 | "(?\\d+)-FROM-(?(-|\\+|)\\d+)-TO-(?" + 29 | "(-|\\+|)\\d+)\\s*$"); 30 | private static final BigInteger INT_MAX = BigInteger.valueOf(2147483647L); 31 | private static final BigInteger INT_MIN = BigInteger.valueOf(-2147483648L); 32 | private final int fromFloor; 33 | private final int toFloor; 34 | private final int personId; 35 | 36 | public PersonRequest(int fromFloor, int toFloor, int personId) { 37 | this.fromFloor = fromFloor; 38 | this.toFloor = toFloor; 39 | this.personId = personId; 40 | } 41 | 42 | private static boolean isValidFloor(Integer floor) { 43 | return floor != null && floor != 0 && floor >= -3 && floor <= 20; 44 | } 45 | 46 | private static boolean isValidInteger(String string) { 47 | try { 48 | BigInteger integer = new BigInteger(string); 49 | return integer.compareTo(INT_MAX) <= 0 && integer.compareTo(INT_MIN) >= 0; 50 | } catch (Exception var2) { 51 | return false; 52 | } 53 | } 54 | 55 | private static Integer toValidInteger(String string) { 56 | return isValidInteger(string) ? (new BigInteger(string)).intValue() : 57 | null; 58 | } 59 | 60 | static PersonRequest parse(String string) throws PersonRequestException { 61 | Matcher matcher = PARSE_PATTERN.matcher(string); 62 | if (matcher.matches()) { 63 | String personIdString = matcher.group("personId"); 64 | Integer personId = toValidInteger(personIdString); 65 | if (personId == null) { 66 | throw new InvalidPersonIdException(string); 67 | } else { 68 | String fromFloorString = matcher.group("fromFloor"); 69 | Integer fromFloor = toValidInteger(fromFloorString); 70 | if (!isValidFloor(fromFloor)) { 71 | throw new InvalidFromFloorException(string); 72 | } else { 73 | String toFloorString = matcher.group("toFloor"); 74 | Integer toFloor = toValidInteger(toFloorString); 75 | if (!isValidFloor(toFloor)) { 76 | throw new InvalidToFloorException(string); 77 | } else if (fromFloor == toFloor) { 78 | throw new DuplicatedFromToFloorException(string); 79 | } else { 80 | return new PersonRequest(fromFloor, toFloor, personId); 81 | } 82 | } 83 | } 84 | } else { 85 | throw new InvalidPatternException(string); 86 | } 87 | } 88 | 89 | public int getFromFloor() { 90 | return this.fromFloor; 91 | } 92 | 93 | public int getToFloor() { 94 | return this.toFloor; 95 | } 96 | 97 | public int getPersonId() { 98 | return this.personId; 99 | } 100 | 101 | public String toString() { 102 | return String.format("%d-FROM-%d-TO-%d", this.personId, 103 | this.fromFloor, this.toFloor); 104 | } 105 | 106 | public int hashCode() { 107 | return Arrays.hashCode(new int[]{this.personId, this.fromFloor, 108 | this.toFloor}); 109 | } 110 | 111 | public boolean equals(Object obj) { 112 | if (obj == this) { 113 | return true; 114 | } else if (!(obj instanceof PersonRequest)) { 115 | return false; 116 | } else { 117 | return ((PersonRequest) obj).fromFloor == this.fromFloor 118 | && ((PersonRequest) obj).toFloor == this.toFloor 119 | && ((PersonRequest) obj).personId == this.personId; 120 | } 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/com/oocourse/elevator3/exception/DuplicatedFromToFloorException.java: -------------------------------------------------------------------------------- 1 | // 2 | // Source code recreated from a .class file by IntelliJ IDEA 3 | // (powered by Fernflower decompiler) 4 | // 5 | 6 | package com.oocourse.elevator3.exception; 7 | 8 | public class DuplicatedFromToFloorException extends PersonRequestException { 9 | public DuplicatedFromToFloorException(String original) { 10 | super(original); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/com/oocourse/elevator3/exception/DuplicatedPersonIdException.java: -------------------------------------------------------------------------------- 1 | // 2 | // Source code recreated from a .class file by IntelliJ IDEA 3 | // (powered by Fernflower decompiler) 4 | // 5 | 6 | package com.oocourse.elevator3.exception; 7 | 8 | public class DuplicatedPersonIdException extends PersonRequestException { 9 | public DuplicatedPersonIdException(String original) { 10 | super(original); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/com/oocourse/elevator3/exception/InvalidFromFloorException.java: -------------------------------------------------------------------------------- 1 | // 2 | // Source code recreated from a .class file by IntelliJ IDEA 3 | // (powered by Fernflower decompiler) 4 | // 5 | 6 | package com.oocourse.elevator3.exception; 7 | 8 | public class InvalidFromFloorException extends PersonRequestException { 9 | public InvalidFromFloorException(String original) { 10 | super(original); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/com/oocourse/elevator3/exception/InvalidPatternException.java: -------------------------------------------------------------------------------- 1 | // 2 | // Source code recreated from a .class file by IntelliJ IDEA 3 | // (powered by Fernflower decompiler) 4 | // 5 | 6 | package com.oocourse.elevator3.exception; 7 | 8 | public class InvalidPatternException extends PersonRequestException { 9 | public InvalidPatternException(String original) { 10 | super(original); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/com/oocourse/elevator3/exception/InvalidPersonIdException.java: -------------------------------------------------------------------------------- 1 | // 2 | // Source code recreated from a .class file by IntelliJ IDEA 3 | // (powered by Fernflower decompiler) 4 | // 5 | 6 | package com.oocourse.elevator3.exception; 7 | 8 | public class InvalidPersonIdException extends PersonRequestException { 9 | public InvalidPersonIdException(String original) { 10 | super(original); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/com/oocourse/elevator3/exception/InvalidToFloorException.java: -------------------------------------------------------------------------------- 1 | // 2 | // Source code recreated from a .class file by IntelliJ IDEA 3 | // (powered by Fernflower decompiler) 4 | // 5 | 6 | package com.oocourse.elevator3.exception; 7 | 8 | public class InvalidToFloorException extends PersonRequestException { 9 | public InvalidToFloorException(String original) { 10 | super(original); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/com/oocourse/elevator3/exception/PersonRequestException.java: -------------------------------------------------------------------------------- 1 | // 2 | // Source code recreated from a .class file by IntelliJ IDEA 3 | // (powered by Fernflower decompiler) 4 | // 5 | 6 | package com.oocourse.elevator3.exception; 7 | 8 | public abstract class PersonRequestException extends Exception { 9 | private final String original; 10 | 11 | PersonRequestException(String original) { 12 | super(String.format("Person request parse failed! - \"%s\"", original)); 13 | this.original = original; 14 | } 15 | 16 | public String getOriginal() { 17 | return this.original; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/com/oocourse/timed/TimedInputPair.java: -------------------------------------------------------------------------------- 1 | package com.oocourse.timed; 2 | 3 | public class TimedInputPair { 4 | private String inputLine; 5 | private double time; 6 | 7 | public TimedInputPair(String inputLine, double time) { 8 | this.inputLine = inputLine; 9 | this.time = time; 10 | } 11 | 12 | public String getInputLine() { 13 | return inputLine; 14 | } 15 | 16 | public double getTime() { 17 | return time; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/com/oocourse/timed/TimedMiddleware.java: -------------------------------------------------------------------------------- 1 | package com.oocourse.timed; 2 | 3 | import com.oocourse.TimableOutput; 4 | 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.io.PipedInputStream; 8 | import java.io.PipedOutputStream; 9 | import java.util.LinkedList; 10 | import java.util.List; 11 | import java.util.Scanner; 12 | import java.util.regex.Matcher; 13 | import java.util.regex.Pattern; 14 | 15 | public class TimedMiddleware { 16 | private static final String TIMED_PATTERN_STRING 17 | = "^\\[([0-9]+[.][0-9]*)](.*)$"; 18 | private PipedInputStream inputStream; 19 | 20 | public TimedMiddleware(InputStream stream) { 21 | List pairs = getInputPairs(stream); 22 | PipedOutputStream outputStream = new PipedOutputStream(); 23 | try { 24 | inputStream = new PipedInputStream(outputStream); 25 | } catch (IOException e) { 26 | e.printStackTrace(); 27 | } 28 | TimedWriter writer = new TimedWriter(pairs, outputStream); 29 | TimableOutput.initStartTimestamp(); 30 | new Thread(writer).start(); 31 | } 32 | 33 | private static List getInputPairs(InputStream stream) { 34 | List pairs = new LinkedList<>(); 35 | Scanner scanner = new Scanner(stream); 36 | Pattern pattern = Pattern.compile(TIMED_PATTERN_STRING); 37 | while (scanner.hasNext()) { 38 | String next = scanner.next(); 39 | Matcher matcher = pattern.matcher(next); 40 | if (matcher.find()) { 41 | if (!matcher.group(0).equals(next)) { 42 | throw new Error( 43 | String.format("input %s not matched", next)); 44 | } 45 | double time = Double.valueOf(matcher.group(1)); 46 | String inputLine = matcher.group(2); 47 | pairs.add(new TimedInputPair(inputLine, time)); 48 | } else { 49 | throw new Error( 50 | String.format("input %s not matched", next)); 51 | } 52 | } 53 | return pairs; 54 | } 55 | 56 | public InputStream getParsedInputStream() { 57 | return inputStream; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/com/oocourse/timed/TimedWriter.java: -------------------------------------------------------------------------------- 1 | package com.oocourse.timed; 2 | 3 | import java.io.IOException; 4 | import java.io.OutputStream; 5 | import java.util.List; 6 | 7 | public class TimedWriter implements Runnable { 8 | private final List pairs; 9 | private final OutputStream stream; 10 | 11 | private double curTime; 12 | 13 | TimedWriter(List pairs, OutputStream stream) { 14 | this.pairs = pairs; 15 | this.stream = stream; 16 | curTime = 0.; 17 | } 18 | 19 | @Override 20 | public void run() { 21 | for (TimedInputPair pair : 22 | pairs) { 23 | double time = pair.getTime(); 24 | double delta = time - curTime; 25 | if (delta < 0) { 26 | throw new Error("delta < 0"); 27 | } 28 | try { 29 | Thread.sleep(Math.round(delta * 1000)); 30 | } catch (InterruptedException e) { 31 | e.printStackTrace(); 32 | break; 33 | } 34 | try { 35 | stream.write(pair.getInputLine().getBytes()); 36 | stream.write('\n'); 37 | stream.flush(); 38 | } catch (IOException e) { 39 | e.printStackTrace(); 40 | break; 41 | } 42 | curTime = time; 43 | } 44 | try { 45 | stream.close(); 46 | } catch (IOException e) { 47 | e.printStackTrace(); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /test/com/oocourse/elevator2/ElevatorInputTest.java: -------------------------------------------------------------------------------- 1 | package com.oocourse.elevator2; 2 | 3 | import com.oocourse.TimableOutput; 4 | 5 | public class ElevatorInputTest { 6 | 7 | public static void main(String[] args) { 8 | TimableOutput.initStartTimestamp(); 9 | ElevatorInput input = new ElevatorInput(System.in); 10 | while (true) { 11 | PersonRequest nextInput = input.nextPersonRequest(); 12 | if (nextInput == null) { 13 | break; 14 | } 15 | TimableOutput.println(nextInput); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /test/com/oocourse/elevator3/ElevatorInputTest.java: -------------------------------------------------------------------------------- 1 | package com.oocourse.elevator3; 2 | 3 | import com.oocourse.TimableOutput; 4 | import org.junit.Test; 5 | 6 | import java.io.ByteArrayInputStream; 7 | 8 | public class ElevatorInputTest { 9 | public static void main(String[] args) { 10 | TimableOutput.initStartTimestamp(); 11 | com.oocourse.elevator3.ElevatorInput input = 12 | new ElevatorInput(System.in); 13 | while (true) { 14 | PersonRequest nextInput = input.nextPersonRequest(); 15 | if (nextInput == null) { 16 | break; 17 | } 18 | TimableOutput.println(nextInput); 19 | } 20 | } 21 | 22 | @Test 23 | public void floorRangeTest() { 24 | TimableOutput.initStartTimestamp(); 25 | StringBuilder builder = new StringBuilder(); 26 | builder.append("[0.0]1-FROM-1-TO--3\n"); 27 | builder.append("[0.0]2-FROM--3-TO-1\n"); 28 | builder.append("[0.0]3-FROM-1-TO-20\n"); 29 | builder.append("[0.0]4-FROM-20-TO-1\n"); 30 | com.oocourse.elevator3.ElevatorInput input = 31 | new ElevatorInput(new ByteArrayInputStream( 32 | builder.toString().getBytes() 33 | )); 34 | while (true) { 35 | PersonRequest nextInput = input.nextPersonRequest(); 36 | if (nextInput == null) { 37 | break; 38 | } 39 | } 40 | } 41 | } --------------------------------------------------------------------------------