├── .gitignore ├── LICENSE-CC.txt ├── LICENSE-GPL.txt ├── README.md ├── build.gradle ├── docs ├── CED.html ├── DM.html ├── EACO.html ├── TGERDS.html ├── UEE.html ├── tgerds0.png └── tgerds1.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── notes ├── CED.ipynb ├── DM.ipynb ├── EACO.ipynb ├── TGERDS.ipynb ├── UEE.ipynb ├── notes.py ├── tgerds0.png └── tgerds1.png ├── publish └── src ├── licenseHeaderFile.txt └── main └── java └── it └── unimi └── di └── prog2 └── notes ├── ced ├── MainECC.java ├── MainECE.java ├── MainEF.java ├── ec │ ├── Adder.java │ ├── LogAdderComp.java │ └── LogAdderExt.java ├── ef │ ├── Adder.java │ ├── AdderForwarder.java │ ├── AdderInterface.java │ └── LogAdder.java └── package-info.java ├── eaco ├── MainRS.java ├── MainSR.java ├── package-info.java ├── rs │ ├── Histogram.java │ ├── Rectangle.java │ └── Square.java └── sr │ ├── Histogram.java │ ├── Rectangle.java │ └── Square.java ├── tgerds └── Examples.java └── uee ├── MainLSP.java ├── MainSym.java ├── MainTrans.java ├── lsp ├── Client.java ├── R.java ├── S.java └── T.java ├── package-info.java ├── sym ├── S.java └── T.java └── trans ├── S.java └── T.java /.gitignore: -------------------------------------------------------------------------------- 1 | .envrc 2 | .gradle 3 | build/ 4 | bin/ 5 | -------------------------------------------------------------------------------- /LICENSE-CC.txt: -------------------------------------------------------------------------------- 1 | Attribution-ShareAlike 4.0 International 2 | 3 | ======================================================================= 4 | 5 | Creative Commons Corporation ("Creative Commons") is not a law firm and 6 | does not provide legal services or legal advice. Distribution of 7 | Creative Commons public licenses does not create a lawyer-client or 8 | other relationship. Creative Commons makes its licenses and related 9 | information available on an "as-is" basis. Creative Commons gives no 10 | warranties regarding its licenses, any material licensed under their 11 | terms and conditions, or any related information. Creative Commons 12 | disclaims all liability for damages resulting from their use to the 13 | fullest extent possible. 14 | 15 | Using Creative Commons Public Licenses 16 | 17 | Creative Commons public licenses provide a standard set of terms and 18 | conditions that creators and other rights holders may use to share 19 | original works of authorship and other material subject to copyright 20 | and certain other rights specified in the public license below. The 21 | following considerations are for informational purposes only, are not 22 | exhaustive, and do not form part of our licenses. 23 | 24 | Considerations for licensors: Our public licenses are 25 | intended for use by those authorized to give the public 26 | permission to use material in ways otherwise restricted by 27 | copyright and certain other rights. Our licenses are 28 | irrevocable. Licensors should read and understand the terms 29 | and conditions of the license they choose before applying it. 30 | Licensors should also secure all rights necessary before 31 | applying our licenses so that the public can reuse the 32 | material as expected. Licensors should clearly mark any 33 | material not subject to the license. This includes other CC- 34 | licensed material, or material used under an exception or 35 | limitation to copyright. More considerations for licensors: 36 | wiki.creativecommons.org/Considerations_for_licensors 37 | 38 | Considerations for the public: By using one of our public 39 | licenses, a licensor grants the public permission to use the 40 | licensed material under specified terms and conditions. If 41 | the licensor's permission is not necessary for any reason--for 42 | example, because of any applicable exception or limitation to 43 | copyright--then that use is not regulated by the license. Our 44 | licenses grant only permissions under copyright and certain 45 | other rights that a licensor has authority to grant. Use of 46 | the licensed material may still be restricted for other 47 | reasons, including because others have copyright or other 48 | rights in the material. A licensor may make special requests, 49 | such as asking that all changes be marked or described. 50 | Although not required by our licenses, you are encouraged to 51 | respect those requests where reasonable. More_considerations 52 | for the public: 53 | wiki.creativecommons.org/Considerations_for_licensees 54 | 55 | ======================================================================= 56 | 57 | Creative Commons Attribution-ShareAlike 4.0 International Public 58 | License 59 | 60 | By exercising the Licensed Rights (defined below), You accept and agree 61 | to be bound by the terms and conditions of this Creative Commons 62 | Attribution-ShareAlike 4.0 International Public License ("Public 63 | License"). To the extent this Public License may be interpreted as a 64 | contract, You are granted the Licensed Rights in consideration of Your 65 | acceptance of these terms and conditions, and the Licensor grants You 66 | such rights in consideration of benefits the Licensor receives from 67 | making the Licensed Material available under these terms and 68 | conditions. 69 | 70 | 71 | Section 1 -- Definitions. 72 | 73 | a. Adapted Material means material subject to Copyright and Similar 74 | Rights that is derived from or based upon the Licensed Material 75 | and in which the Licensed Material is translated, altered, 76 | arranged, transformed, or otherwise modified in a manner requiring 77 | permission under the Copyright and Similar Rights held by the 78 | Licensor. For purposes of this Public License, where the Licensed 79 | Material is a musical work, performance, or sound recording, 80 | Adapted Material is always produced where the Licensed Material is 81 | synched in timed relation with a moving image. 82 | 83 | b. Adapter's License means the license You apply to Your Copyright 84 | and Similar Rights in Your contributions to Adapted Material in 85 | accordance with the terms and conditions of this Public License. 86 | 87 | c. BY-SA Compatible License means a license listed at 88 | creativecommons.org/compatiblelicenses, approved by Creative 89 | Commons as essentially the equivalent of this Public License. 90 | 91 | d. Copyright and Similar Rights means copyright and/or similar rights 92 | closely related to copyright including, without limitation, 93 | performance, broadcast, sound recording, and Sui Generis Database 94 | Rights, without regard to how the rights are labeled or 95 | categorized. For purposes of this Public License, the rights 96 | specified in Section 2(b)(1)-(2) are not Copyright and Similar 97 | Rights. 98 | 99 | e. Effective Technological Measures means those measures that, in the 100 | absence of proper authority, may not be circumvented under laws 101 | fulfilling obligations under Article 11 of the WIPO Copyright 102 | Treaty adopted on December 20, 1996, and/or similar international 103 | agreements. 104 | 105 | f. Exceptions and Limitations means fair use, fair dealing, and/or 106 | any other exception or limitation to Copyright and Similar Rights 107 | that applies to Your use of the Licensed Material. 108 | 109 | g. License Elements means the license attributes listed in the name 110 | of a Creative Commons Public License. The License Elements of this 111 | Public License are Attribution and ShareAlike. 112 | 113 | h. Licensed Material means the artistic or literary work, database, 114 | or other material to which the Licensor applied this Public 115 | License. 116 | 117 | i. Licensed Rights means the rights granted to You subject to the 118 | terms and conditions of this Public License, which are limited to 119 | all Copyright and Similar Rights that apply to Your use of the 120 | Licensed Material and that the Licensor has authority to license. 121 | 122 | j. Licensor means the individual(s) or entity(ies) granting rights 123 | under this Public License. 124 | 125 | k. Share means to provide material to the public by any means or 126 | process that requires permission under the Licensed Rights, such 127 | as reproduction, public display, public performance, distribution, 128 | dissemination, communication, or importation, and to make material 129 | available to the public including in ways that members of the 130 | public may access the material from a place and at a time 131 | individually chosen by them. 132 | 133 | l. Sui Generis Database Rights means rights other than copyright 134 | resulting from Directive 96/9/EC of the European Parliament and of 135 | the Council of 11 March 1996 on the legal protection of databases, 136 | as amended and/or succeeded, as well as other essentially 137 | equivalent rights anywhere in the world. 138 | 139 | m. You means the individual or entity exercising the Licensed Rights 140 | under this Public License. Your has a corresponding meaning. 141 | 142 | 143 | Section 2 -- Scope. 144 | 145 | a. License grant. 146 | 147 | 1. Subject to the terms and conditions of this Public License, 148 | the Licensor hereby grants You a worldwide, royalty-free, 149 | non-sublicensable, non-exclusive, irrevocable license to 150 | exercise the Licensed Rights in the Licensed Material to: 151 | 152 | a. reproduce and Share the Licensed Material, in whole or 153 | in part; and 154 | 155 | b. produce, reproduce, and Share Adapted Material. 156 | 157 | 2. Exceptions and Limitations. For the avoidance of doubt, where 158 | Exceptions and Limitations apply to Your use, this Public 159 | License does not apply, and You do not need to comply with 160 | its terms and conditions. 161 | 162 | 3. Term. The term of this Public License is specified in Section 163 | 6(a). 164 | 165 | 4. Media and formats; technical modifications allowed. The 166 | Licensor authorizes You to exercise the Licensed Rights in 167 | all media and formats whether now known or hereafter created, 168 | and to make technical modifications necessary to do so. The 169 | Licensor waives and/or agrees not to assert any right or 170 | authority to forbid You from making technical modifications 171 | necessary to exercise the Licensed Rights, including 172 | technical modifications necessary to circumvent Effective 173 | Technological Measures. For purposes of this Public License, 174 | simply making modifications authorized by this Section 2(a) 175 | (4) never produces Adapted Material. 176 | 177 | 5. Downstream recipients. 178 | 179 | a. Offer from the Licensor -- Licensed Material. Every 180 | recipient of the Licensed Material automatically 181 | receives an offer from the Licensor to exercise the 182 | Licensed Rights under the terms and conditions of this 183 | Public License. 184 | 185 | b. Additional offer from the Licensor -- Adapted Material. 186 | Every recipient of Adapted Material from You 187 | automatically receives an offer from the Licensor to 188 | exercise the Licensed Rights in the Adapted Material 189 | under the conditions of the Adapter's License You apply. 190 | 191 | c. No downstream restrictions. You may not offer or impose 192 | any additional or different terms or conditions on, or 193 | apply any Effective Technological Measures to, the 194 | Licensed Material if doing so restricts exercise of the 195 | Licensed Rights by any recipient of the Licensed 196 | Material. 197 | 198 | 6. No endorsement. Nothing in this Public License constitutes or 199 | may be construed as permission to assert or imply that You 200 | are, or that Your use of the Licensed Material is, connected 201 | with, or sponsored, endorsed, or granted official status by, 202 | the Licensor or others designated to receive attribution as 203 | provided in Section 3(a)(1)(A)(i). 204 | 205 | b. Other rights. 206 | 207 | 1. Moral rights, such as the right of integrity, are not 208 | licensed under this Public License, nor are publicity, 209 | privacy, and/or other similar personality rights; however, to 210 | the extent possible, the Licensor waives and/or agrees not to 211 | assert any such rights held by the Licensor to the limited 212 | extent necessary to allow You to exercise the Licensed 213 | Rights, but not otherwise. 214 | 215 | 2. Patent and trademark rights are not licensed under this 216 | Public License. 217 | 218 | 3. To the extent possible, the Licensor waives any right to 219 | collect royalties from You for the exercise of the Licensed 220 | Rights, whether directly or through a collecting society 221 | under any voluntary or waivable statutory or compulsory 222 | licensing scheme. In all other cases the Licensor expressly 223 | reserves any right to collect such royalties. 224 | 225 | 226 | Section 3 -- License Conditions. 227 | 228 | Your exercise of the Licensed Rights is expressly made subject to the 229 | following conditions. 230 | 231 | a. Attribution. 232 | 233 | 1. If You Share the Licensed Material (including in modified 234 | form), You must: 235 | 236 | a. retain the following if it is supplied by the Licensor 237 | with the Licensed Material: 238 | 239 | i. identification of the creator(s) of the Licensed 240 | Material and any others designated to receive 241 | attribution, in any reasonable manner requested by 242 | the Licensor (including by pseudonym if 243 | designated); 244 | 245 | ii. a copyright notice; 246 | 247 | iii. a notice that refers to this Public License; 248 | 249 | iv. a notice that refers to the disclaimer of 250 | warranties; 251 | 252 | v. a URI or hyperlink to the Licensed Material to the 253 | extent reasonably practicable; 254 | 255 | b. indicate if You modified the Licensed Material and 256 | retain an indication of any previous modifications; and 257 | 258 | c. indicate the Licensed Material is licensed under this 259 | Public License, and include the text of, or the URI or 260 | hyperlink to, this Public License. 261 | 262 | 2. You may satisfy the conditions in Section 3(a)(1) in any 263 | reasonable manner based on the medium, means, and context in 264 | which You Share the Licensed Material. For example, it may be 265 | reasonable to satisfy the conditions by providing a URI or 266 | hyperlink to a resource that includes the required 267 | information. 268 | 269 | 3. If requested by the Licensor, You must remove any of the 270 | information required by Section 3(a)(1)(A) to the extent 271 | reasonably practicable. 272 | 273 | b. ShareAlike. 274 | 275 | In addition to the conditions in Section 3(a), if You Share 276 | Adapted Material You produce, the following conditions also apply. 277 | 278 | 1. The Adapter's License You apply must be a Creative Commons 279 | license with the same License Elements, this version or 280 | later, or a BY-SA Compatible License. 281 | 282 | 2. You must include the text of, or the URI or hyperlink to, the 283 | Adapter's License You apply. You may satisfy this condition 284 | in any reasonable manner based on the medium, means, and 285 | context in which You Share Adapted Material. 286 | 287 | 3. You may not offer or impose any additional or different terms 288 | or conditions on, or apply any Effective Technological 289 | Measures to, Adapted Material that restrict exercise of the 290 | rights granted under the Adapter's License You apply. 291 | 292 | 293 | Section 4 -- Sui Generis Database Rights. 294 | 295 | Where the Licensed Rights include Sui Generis Database Rights that 296 | apply to Your use of the Licensed Material: 297 | 298 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right 299 | to extract, reuse, reproduce, and Share all or a substantial 300 | portion of the contents of the database; 301 | 302 | b. if You include all or a substantial portion of the database 303 | contents in a database in which You have Sui Generis Database 304 | Rights, then the database in which You have Sui Generis Database 305 | Rights (but not its individual contents) is Adapted Material, 306 | 307 | including for purposes of Section 3(b); and 308 | c. You must comply with the conditions in Section 3(a) if You Share 309 | all or a substantial portion of the contents of the database. 310 | 311 | For the avoidance of doubt, this Section 4 supplements and does not 312 | replace Your obligations under this Public License where the Licensed 313 | Rights include other Copyright and Similar Rights. 314 | 315 | 316 | Section 5 -- Disclaimer of Warranties and Limitation of Liability. 317 | 318 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE 319 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS 320 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF 321 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, 322 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, 323 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR 324 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, 325 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT 326 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT 327 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. 328 | 329 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE 330 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, 331 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, 332 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, 333 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR 334 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN 335 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR 336 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR 337 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. 338 | 339 | c. The disclaimer of warranties and limitation of liability provided 340 | above shall be interpreted in a manner that, to the extent 341 | possible, most closely approximates an absolute disclaimer and 342 | waiver of all liability. 343 | 344 | 345 | Section 6 -- Term and Termination. 346 | 347 | a. This Public License applies for the term of the Copyright and 348 | Similar Rights licensed here. However, if You fail to comply with 349 | this Public License, then Your rights under this Public License 350 | terminate automatically. 351 | 352 | b. Where Your right to use the Licensed Material has terminated under 353 | Section 6(a), it reinstates: 354 | 355 | 1. automatically as of the date the violation is cured, provided 356 | it is cured within 30 days of Your discovery of the 357 | violation; or 358 | 359 | 2. upon express reinstatement by the Licensor. 360 | 361 | For the avoidance of doubt, this Section 6(b) does not affect any 362 | right the Licensor may have to seek remedies for Your violations 363 | of this Public License. 364 | 365 | c. For the avoidance of doubt, the Licensor may also offer the 366 | Licensed Material under separate terms or conditions or stop 367 | distributing the Licensed Material at any time; however, doing so 368 | will not terminate this Public License. 369 | 370 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public 371 | License. 372 | 373 | 374 | Section 7 -- Other Terms and Conditions. 375 | 376 | a. The Licensor shall not be bound by any additional or different 377 | terms or conditions communicated by You unless expressly agreed. 378 | 379 | b. Any arrangements, understandings, or agreements regarding the 380 | Licensed Material not stated herein are separate from and 381 | independent of the terms and conditions of this Public License. 382 | 383 | 384 | Section 8 -- Interpretation. 385 | 386 | a. For the avoidance of doubt, this Public License does not, and 387 | shall not be interpreted to, reduce, limit, restrict, or impose 388 | conditions on any use of the Licensed Material that could lawfully 389 | be made without permission under this Public License. 390 | 391 | b. To the extent possible, if any provision of this Public License is 392 | deemed unenforceable, it shall be automatically reformed to the 393 | minimum extent necessary to make it enforceable. If the provision 394 | cannot be reformed, it shall be severed from this Public License 395 | without affecting the enforceability of the remaining terms and 396 | conditions. 397 | 398 | c. No term or condition of this Public License will be waived and no 399 | failure to comply consented to unless expressly agreed to by the 400 | Licensor. 401 | 402 | d. Nothing in this Public License constitutes or may be interpreted 403 | as a limitation upon, or waiver of, any privileges and immunities 404 | that apply to the Licensor or You, including from the legal 405 | processes of any jurisdiction or authority. 406 | 407 | 408 | ======================================================================= 409 | 410 | Creative Commons is not a party to its public 411 | licenses. Notwithstanding, Creative Commons may elect to apply one of 412 | its public licenses to material it publishes and in those instances 413 | will be considered the “Licensor.” The text of the Creative Commons 414 | public licenses is dedicated to the public domain under the CC0 Public 415 | Domain Dedication. Except for the limited purpose of indicating that 416 | material is shared under a Creative Commons public license or as 417 | otherwise permitted by the Creative Commons policies published at 418 | creativecommons.org/policies, Creative Commons does not authorize the 419 | use of the trademark "Creative Commons" or any other trademark or logo 420 | of Creative Commons without its prior written consent including, 421 | without limitation, in connection with any unauthorized modifications 422 | to any of its public licenses or any other arrangements, 423 | understandings, or agreements concerning use of licensed material. For 424 | the avoidance of doubt, this paragraph does not form part of the 425 | public licenses. 426 | 427 | Creative Commons may be contacted at creativecommons.org. 428 | 429 | -------------------------------------------------------------------------------- /LICENSE-GPL.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Prog2@UniMI Notes 2 | 3 | [![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](http://www.gnu.org/licenses/gpl-3.0) 4 | [![License: CC BY-SA 4.0](https://img.shields.io/badge/License-CC%20BY--SA%204.0-blue.svg)](http://creativecommons.org/licenses/by-sa/4.0/) 5 | 6 | Questo repository le seguenti *note* di approfondimento riguardanti l'insegnamento di [Programmazione II](https://prog2.di.unimi.it/) del corso di laurea in [Informatica](https://informatica.cdl.unimi.it/it) dell'[Università degli Studi di Milano](http://www.unimi.it/): 7 | 8 | * [Il meccanismo di dispatching](https://prog2-unimi.github.io/notes/DM.html); 9 | * [Ereditarietà a carattere ontologico](https://prog2-unimi.github.io/notes/EACO.html); 10 | * [Uguaglianza ed ereditarietà](https://prog2-unimi.github.io/notes/UEE.html); 11 | * [Composizione e delega](https://prog2-unimi.github.io/notes/CED.html); 12 | * [Tipi generici e relazioni di sottotipo](https://prog2-unimi.github.io/notes/TGERDS.html). 13 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'com.diffplug.spotless' version '6.25.0' 4 | } 5 | 6 | java { 7 | toolchain { 8 | languageVersion = JavaLanguageVersion.of(21) 9 | } 10 | } 11 | 12 | compileJava.options.encoding = 'UTF-8' 13 | compileTestJava.options.encoding = 'UTF-8' 14 | 15 | repositories { 16 | mavenLocal() 17 | mavenCentral() 18 | } 19 | 20 | tasks.withType(JavaCompile) { 21 | options.encoding = 'UTF-8' 22 | options.compilerArgs.add('-Xlint:all') 23 | } 24 | 25 | javadoc { 26 | exclude('utils/**', 'clients/**') 27 | options { 28 | description = 'Generates Javadoc API documentation and UML diagrams for the main source code.' 29 | encoding = 'UTF-8' 30 | charSet = 'UTF-8' 31 | links = [ 'https://docs.oracle.com/en/java/javase/21/docs/api/' ] 32 | docTitle = 'Programmazione II @ UniMI' 33 | overview = 'src/overview.html' 34 | header = '' 35 | showFromPrivate() 36 | addBooleanOption 'Xdoclint:all', true 37 | addBooleanOption '-allow-script-in-comments', true 38 | } 39 | } 40 | 41 | spotless { 42 | enforceCheck false 43 | java { 44 | googleJavaFormat('1.24.0') 45 | removeUnusedImports() 46 | indentWithSpaces(2) 47 | endWithNewline() 48 | licenseHeaderFile 'src/licenseHeaderFile.txt' 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /docs/tgerds0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prog2-unimi/notes/e62ff5bf29b6bf2c4401b6e59970f9ecfab4f298/docs/tgerds0.png -------------------------------------------------------------------------------- /docs/tgerds1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prog2-unimi/notes/e62ff5bf29b6bf2c4401b6e59970f9ecfab4f298/docs/tgerds1.png -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prog2-unimi/notes/e62ff5bf29b6bf2c4401b6e59970f9ecfab4f298/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | # SPDX-License-Identifier: Apache-2.0 19 | # 20 | 21 | ############################################################################## 22 | # 23 | # Gradle start up script for POSIX generated by Gradle. 24 | # 25 | # Important for running: 26 | # 27 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 28 | # noncompliant, but you have some other compliant shell such as ksh or 29 | # bash, then to run this script, type that shell name before the whole 30 | # command line, like: 31 | # 32 | # ksh Gradle 33 | # 34 | # Busybox and similar reduced shells will NOT work, because this script 35 | # requires all of these POSIX shell features: 36 | # * functions; 37 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 38 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 39 | # * compound commands having a testable exit status, especially «case»; 40 | # * various built-in commands including «command», «set», and «ulimit». 41 | # 42 | # Important for patching: 43 | # 44 | # (2) This script targets any POSIX shell, so it avoids extensions provided 45 | # by Bash, Ksh, etc; in particular arrays are avoided. 46 | # 47 | # The "traditional" practice of packing multiple parameters into a 48 | # space-separated string is a well documented source of bugs and security 49 | # problems, so this is (mostly) avoided, by progressively accumulating 50 | # options in "$@", and eventually passing that to Java. 51 | # 52 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 53 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 54 | # see the in-line comments for details. 55 | # 56 | # There are tweaks for specific operating systems such as AIX, CygWin, 57 | # Darwin, MinGW, and NonStop. 58 | # 59 | # (3) This script is generated from the Groovy template 60 | # https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 61 | # within the Gradle project. 62 | # 63 | # You can find Gradle at https://github.com/gradle/gradle/. 64 | # 65 | ############################################################################## 66 | 67 | # Attempt to set APP_HOME 68 | 69 | # Resolve links: $0 may be a link 70 | app_path=$0 71 | 72 | # Need this for daisy-chained symlinks. 73 | while 74 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 75 | [ -h "$app_path" ] 76 | do 77 | ls=$( ls -ld "$app_path" ) 78 | link=${ls#*' -> '} 79 | case $link in #( 80 | /*) app_path=$link ;; #( 81 | *) app_path=$APP_HOME$link ;; 82 | esac 83 | done 84 | 85 | # This is normally unused 86 | # shellcheck disable=SC2034 87 | APP_BASE_NAME=${0##*/} 88 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) 89 | APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s 90 | ' "$PWD" ) || exit 91 | 92 | # Use the maximum available, or set MAX_FD != -1 to use that value. 93 | MAX_FD=maximum 94 | 95 | warn () { 96 | echo "$*" 97 | } >&2 98 | 99 | die () { 100 | echo 101 | echo "$*" 102 | echo 103 | exit 1 104 | } >&2 105 | 106 | # OS specific support (must be 'true' or 'false'). 107 | cygwin=false 108 | msys=false 109 | darwin=false 110 | nonstop=false 111 | case "$( uname )" in #( 112 | CYGWIN* ) cygwin=true ;; #( 113 | Darwin* ) darwin=true ;; #( 114 | MSYS* | MINGW* ) msys=true ;; #( 115 | NONSTOP* ) nonstop=true ;; 116 | esac 117 | 118 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 119 | 120 | 121 | # Determine the Java command to use to start the JVM. 122 | if [ -n "$JAVA_HOME" ] ; then 123 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 124 | # IBM's JDK on AIX uses strange locations for the executables 125 | JAVACMD=$JAVA_HOME/jre/sh/java 126 | else 127 | JAVACMD=$JAVA_HOME/bin/java 128 | fi 129 | if [ ! -x "$JAVACMD" ] ; then 130 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 131 | 132 | Please set the JAVA_HOME variable in your environment to match the 133 | location of your Java installation." 134 | fi 135 | else 136 | JAVACMD=java 137 | if ! command -v java >/dev/null 2>&1 138 | then 139 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 140 | 141 | Please set the JAVA_HOME variable in your environment to match the 142 | location of your Java installation." 143 | fi 144 | fi 145 | 146 | # Increase the maximum file descriptors if we can. 147 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 148 | case $MAX_FD in #( 149 | max*) 150 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 151 | # shellcheck disable=SC2039,SC3045 152 | MAX_FD=$( ulimit -H -n ) || 153 | warn "Could not query maximum file descriptor limit" 154 | esac 155 | case $MAX_FD in #( 156 | '' | soft) :;; #( 157 | *) 158 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 159 | # shellcheck disable=SC2039,SC3045 160 | ulimit -n "$MAX_FD" || 161 | warn "Could not set maximum file descriptor limit to $MAX_FD" 162 | esac 163 | fi 164 | 165 | # Collect all arguments for the java command, stacking in reverse order: 166 | # * args from the command line 167 | # * the main class name 168 | # * -classpath 169 | # * -D...appname settings 170 | # * --module-path (only if needed) 171 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 172 | 173 | # For Cygwin or MSYS, switch paths to Windows format before running java 174 | if "$cygwin" || "$msys" ; then 175 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 176 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 177 | 178 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 179 | 180 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 181 | for arg do 182 | if 183 | case $arg in #( 184 | -*) false ;; # don't mess with options #( 185 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 186 | [ -e "$t" ] ;; #( 187 | *) false ;; 188 | esac 189 | then 190 | arg=$( cygpath --path --ignore --mixed "$arg" ) 191 | fi 192 | # Roll the args list around exactly as many times as the number of 193 | # args, so each arg winds up back in the position where it started, but 194 | # possibly modified. 195 | # 196 | # NB: a `for` loop captures its iteration list before it begins, so 197 | # changing the positional parameters here affects neither the number of 198 | # iterations, nor the values presented in `arg`. 199 | shift # remove old arg 200 | set -- "$@" "$arg" # push replacement arg 201 | done 202 | fi 203 | 204 | 205 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 206 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 207 | 208 | # Collect all arguments for the java command: 209 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, 210 | # and any embedded shellness will be escaped. 211 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be 212 | # treated as '${Hostname}' itself on the command line. 213 | 214 | set -- \ 215 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 216 | -classpath "$CLASSPATH" \ 217 | org.gradle.wrapper.GradleWrapperMain \ 218 | "$@" 219 | 220 | # Stop when "xargs" is not available. 221 | if ! command -v xargs >/dev/null 2>&1 222 | then 223 | die "xargs is not available" 224 | fi 225 | 226 | # Use "xargs" to parse quoted args. 227 | # 228 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 229 | # 230 | # In Bash we could simply go: 231 | # 232 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 233 | # set -- "${ARGS[@]}" "$@" 234 | # 235 | # but POSIX shell has neither arrays nor command substitution, so instead we 236 | # post-process each arg (as a line of input to sed) to backslash-escape any 237 | # character that might be a shell metacharacter, then use eval to reverse 238 | # that process (while maintaining the separation between arguments), and wrap 239 | # the whole thing up as a single "set" statement. 240 | # 241 | # This will of course break if any of these variables contains a newline or 242 | # an unmatched quote. 243 | # 244 | 245 | eval "set -- $( 246 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 247 | xargs -n1 | 248 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 249 | tr '\n' ' ' 250 | )" '"$@"' 251 | 252 | exec "$JAVACMD" "$@" 253 | -------------------------------------------------------------------------------- /notes/DM.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Il meccanismo di *dispatching*\n", 8 | "\n", 9 | "Questa breve nota intende illustrare, attraverso una serie di esempi commentati, il meccanismo di *dispatching* attraverso il quale viene dapprima selezionata la segnatura del metodo da invocare (durante la fase di compilazione) e quindi individuata l'implementazione da eseguire (durante la fase di esecuzione). \n", 10 | "\n", 11 | "Il caso più elementare è quello di una singola classe in cui ci sia un solo metodo con un dato nome:" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": { 18 | "vscode": { 19 | "languageId": "java" 20 | } 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "public class Simple {\n", 25 | " public void f() {\n", 26 | " System.out.println(\"Simple::f\");\n", 27 | " }\n", 28 | "}" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "In queste circostanze, è evidente che il tipo *apparente* e *concerto* non possono che coincidere e che l'invocazoine di `f` su un oggetto di tipo `Simple` non può che produrre l'invocazione dell'unica implementazione esistente." 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 2, 41 | "metadata": { 42 | "vscode": { 43 | "languageId": "java" 44 | } 45 | }, 46 | "outputs": [ 47 | { 48 | "name": "stdout", 49 | "output_type": "stream", 50 | "text": [ 51 | "Simple::f\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "Simple s = new Simple();\n", 57 | "s.f();" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "## Overloading\n", 65 | "\n", 66 | "Se vengono definiti più metodi col medesimo nome, ossia c'è un di *overloading*, la selezione della segnatura del metodo da invocare segue la logica del minor numero di conversioni (il compilatore tenta di individuare la segnatura detta *most specific*)." 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 3, 72 | "metadata": { 73 | "vscode": { 74 | "languageId": "java" 75 | } 76 | }, 77 | "outputs": [], 78 | "source": [ 79 | "public class Overload {\n", 80 | " public void f(int x) {\n", 81 | " System.out.println(\"Overload::f(int)\");\n", 82 | " }\n", 83 | " public void f(double x) {\n", 84 | " System.out.println(\"Overload::f(double)\");\n", 85 | " }\n", 86 | "}" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "In alcuni casi il numero di conversioni è zero" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 4, 99 | "metadata": { 100 | "vscode": { 101 | "languageId": "java" 102 | } 103 | }, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "Overload::f(int)\n", 110 | "Overload::f(double)\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "Overload o = new Overload();\n", 116 | "o.f(1);\n", 117 | "o.f(1.0); // 1.0 è un double, nessuna conversione" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "mentre in altri è sufficiente effettuare una conversione (da `float` a `double`) e non ci sono altre possibilità" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 5, 130 | "metadata": { 131 | "vscode": { 132 | "languageId": "java" 133 | } 134 | }, 135 | "outputs": [ 136 | { 137 | "name": "stdout", 138 | "output_type": "stream", 139 | "text": [ 140 | "Overload::f(double)\n" 141 | ] 142 | } 143 | ], 144 | "source": [ 145 | "o.f(1.0f); // 1.0f è un float, una conversione" 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "metadata": {}, 151 | "source": [ 152 | "Le cose si complicano se, date le segnature dei metodi e i tipi dei parametri concreti nell'invocazione, esiste più di una segnatura che si adattarebbe alla chiamata a parità di numero di conversioni." 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 6, 158 | "metadata": { 159 | "vscode": { 160 | "languageId": "java" 161 | } 162 | }, 163 | "outputs": [], 164 | "source": [ 165 | "public class Overload {\n", 166 | " public void f(int x, double y) {\n", 167 | " System.out.println(\"Overload::f(int, double)\");\n", 168 | " }\n", 169 | " public void f(double x, int y) {\n", 170 | " System.out.println(\"Overload::f(double, int)\");\n", 171 | " }\n", 172 | "}" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "Usando due `int` entrambe i metodi sono invocabili con una conversione a `double` (del primo, o del secondo argomento)" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 7, 185 | "metadata": { 186 | "vscode": { 187 | "languageId": "java" 188 | } 189 | }, 190 | "outputs": [ 191 | { 192 | "ename": "CompilationException", 193 | "evalue": "", 194 | "output_type": "error", 195 | "traceback": [ 196 | "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[0m\u001b[1m\u001b[30m\u001b[41mo.f\u001b[0m\u001b[1m\u001b[30m(1, 1);\u001b[0m", 197 | "\u001b[1m\u001b[31mreference to f is ambiguous\u001b[0m", 198 | "\u001b[1m\u001b[31m both method f(int,double) in Overload and method f(double,int) in Overload match\u001b[0m", 199 | "" 200 | ] 201 | } 202 | ], 203 | "source": [ 204 | "Overload o = new Overload();\n", 205 | "o.f(1, 1);" 206 | ] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "metadata": {}, 211 | "source": [ 212 | "In questo caso non è possibile individuare la segnatura *most specific*, il compialtore non può scegliere quale segnatura selezione! \n", 213 | "\n", 214 | "È utile ribadire che ciò dipende dal tipo di invocazione: è evidente che le invocazioni che non causano conversioni, ad esempio, sono entrambe legittime." 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 8, 220 | "metadata": { 221 | "vscode": { 222 | "languageId": "java" 223 | } 224 | }, 225 | "outputs": [ 226 | { 227 | "name": "stdout", 228 | "output_type": "stream", 229 | "text": [ 230 | "Overload::f(int, double)\n", 231 | "Overload::f(double, int)\n" 232 | ] 233 | } 234 | ], 235 | "source": [ 236 | "o.f(1, 1.0);\n", 237 | "o.f(1.0, 0);" 238 | ] 239 | }, 240 | { 241 | "cell_type": "markdown", 242 | "metadata": {}, 243 | "source": [ 244 | "## Ereditarietà\n", 245 | "\n", 246 | "L'introduzione di un sottotipo apre, tra le altre, la possibilità che su un oggetto di un sottotipo venga invocato un metodo definito nel supertipo." 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 9, 252 | "metadata": { 253 | "vscode": { 254 | "languageId": "java" 255 | } 256 | }, 257 | "outputs": [], 258 | "source": [ 259 | "public class Above {\n", 260 | " public void f() {\n", 261 | " System.out.println(\"Above::f\");\n", 262 | " }\n", 263 | "}\n", 264 | "\n", 265 | "public class Below extends Above {\n", 266 | " public void g() {\n", 267 | " System.out.println(\"Below::g\");\n", 268 | " }\n", 269 | "}" 270 | ] 271 | }, 272 | { 273 | "cell_type": "markdown", 274 | "metadata": {}, 275 | "source": [ 276 | "Per prima cosa, osserviamo che, considerando anche i sottotipi, il tipo apparente e concreto non necessariamente coincidono: si aprono tre possibilità a seconda che il tipo apparente e concreto siano, rispettivamente\n", 277 | "\n", 278 | "* `Above` e `Above`,\n", 279 | "* `Above` e `Below`,\n", 280 | "* `Below` e `Below`\n", 281 | "\n", 282 | "evidentemente il caso `Below` e `Above` non è possibile in quanto il secondo non è sottotipo del primo.\n", 283 | "\n", 284 | "Nel primo caso è possibile solo l'invocazione di `f`" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 10, 290 | "metadata": { 291 | "vscode": { 292 | "languageId": "java" 293 | } 294 | }, 295 | "outputs": [ 296 | { 297 | "name": "stdout", 298 | "output_type": "stream", 299 | "text": [ 300 | "Above::f\n" 301 | ] 302 | } 303 | ], 304 | "source": [ 305 | "Above aa = new Above();\n", 306 | "aa.f();" 307 | ] 308 | }, 309 | { 310 | "cell_type": "markdown", 311 | "metadata": {}, 312 | "source": [ 313 | "ma non quella di `g`, dato non è definita in tale tipo" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 11, 319 | "metadata": { 320 | "vscode": { 321 | "languageId": "java" 322 | } 323 | }, 324 | "outputs": [ 325 | { 326 | "ename": "CompilationException", 327 | "evalue": "", 328 | "output_type": "error", 329 | "traceback": [ 330 | "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[0m\u001b[1m\u001b[30m\u001b[41maa.g\u001b[0m\u001b[1m\u001b[30m();\u001b[0m", 331 | "\u001b[1m\u001b[31mcannot find symbol\u001b[0m", 332 | "\u001b[1m\u001b[31m symbol: method g()\u001b[0m", 333 | "" 334 | ] 335 | } 336 | ], 337 | "source": [ 338 | "aa.g();" 339 | ] 340 | }, 341 | { 342 | "cell_type": "markdown", 343 | "metadata": {}, 344 | "source": [ 345 | "Nel secondo caso, è possibile invocare `f` perché è visibile (a partire dal tipo apparente) e la sua implementazione (nel tipo concreto) viene ereditata dal supertipo" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 12, 351 | "metadata": { 352 | "vscode": { 353 | "languageId": "java" 354 | } 355 | }, 356 | "outputs": [ 357 | { 358 | "name": "stdout", 359 | "output_type": "stream", 360 | "text": [ 361 | "Above::f\n" 362 | ] 363 | } 364 | ], 365 | "source": [ 366 | "Above ab = new Below();\n", 367 | "ab.f();" 368 | ] 369 | }, 370 | { 371 | "cell_type": "markdown", 372 | "metadata": {}, 373 | "source": [ 374 | "Sebbene nel tipo concreto sia definita `g`, il compilatore sceglie il metodo sulla base del tipo apparente, quindi la chiamata di `g` resta impossibile." 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": 13, 380 | "metadata": { 381 | "vscode": { 382 | "languageId": "java" 383 | } 384 | }, 385 | "outputs": [ 386 | { 387 | "ename": "CompilationException", 388 | "evalue": "", 389 | "output_type": "error", 390 | "traceback": [ 391 | "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[0m\u001b[1m\u001b[30m\u001b[41mab.g\u001b[0m\u001b[1m\u001b[30m();\u001b[0m", 392 | "\u001b[1m\u001b[31mcannot find symbol\u001b[0m", 393 | "\u001b[1m\u001b[31m symbol: method g()\u001b[0m", 394 | "" 395 | ] 396 | } 397 | ], 398 | "source": [ 399 | "ab.g();" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "Nel caso del sottotipo, invece, `g` è visibile poiché è definito in tale tipo (e `f` lo è perché ereditata), quindi sono possibili entrambe le invocazioni." 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 14, 412 | "metadata": { 413 | "vscode": { 414 | "languageId": "java" 415 | } 416 | }, 417 | "outputs": [ 418 | { 419 | "name": "stdout", 420 | "output_type": "stream", 421 | "text": [ 422 | "Above::f\n", 423 | "Below::g\n" 424 | ] 425 | } 426 | ], 427 | "source": [ 428 | "Below bb = new Below();\n", 429 | "bb.f();\n", 430 | "bb.g();" 431 | ] 432 | }, 433 | { 434 | "cell_type": "markdown", 435 | "metadata": {}, 436 | "source": [ 437 | "## Overriding\n", 438 | "\n", 439 | "Un sottotipo può decidere di riscrivere l'implementazione di un metodo ereditato, ossia farne l'*overriding*." 440 | ] 441 | }, 442 | { 443 | "cell_type": "code", 444 | "execution_count": 15, 445 | "metadata": { 446 | "vscode": { 447 | "languageId": "java" 448 | } 449 | }, 450 | "outputs": [], 451 | "source": [ 452 | "public class Above {\n", 453 | " public void f(double x) {\n", 454 | " System.out.println(\"Above::f(double)\");\n", 455 | " }\n", 456 | "}\n", 457 | "\n", 458 | "public class Below extends Above {\n", 459 | " @Override\n", 460 | " public void f(double x) {\n", 461 | " System.out.println(\"Below::f(double)\");\n", 462 | " }\n", 463 | "}" 464 | ] 465 | }, 466 | { 467 | "cell_type": "markdown", 468 | "metadata": {}, 469 | "source": [ 470 | "Affinché ciò avvenga, è però necessario che il metodo riscritto abbia la medesima segnatura di quello nel supertipo. \n", 471 | "\n", 472 | "Usando l'annotazione `@Override`, che serve ad esprimere l'intenzione del programmatore, il compilatore può accorgersi e segnalare, nel caso in cui la segnatura fosse diversa, che la nuova implementazione non è davvero un *override* (ma solo un *overload* come sarà chiarito nella prossima sezione)!" 473 | ] 474 | }, 475 | { 476 | "cell_type": "code", 477 | "execution_count": 16, 478 | "metadata": { 479 | "vscode": { 480 | "languageId": "java" 481 | } 482 | }, 483 | "outputs": [ 484 | { 485 | "ename": "CompilationException", 486 | "evalue": "", 487 | "output_type": "error", 488 | "traceback": [ 489 | "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m \u001b[0m\u001b[1m\u001b[30m\u001b[41m@Override\u001b[0m\u001b[1m\u001b[30m\u001b[0m", 490 | "\u001b[1m\u001b[31mmethod does not override or implement a method from a supertype\u001b[0m", 491 | "" 492 | ] 493 | } 494 | ], 495 | "source": [ 496 | "public class BelowErr extends Above {\n", 497 | " @Override\n", 498 | " public void f(int x) {\n", 499 | " System.out.println(\"BelowErr::f(int)\");\n", 500 | " }\n", 501 | "}" 502 | ] 503 | }, 504 | { 505 | "cell_type": "markdown", 506 | "metadata": {}, 507 | "source": [ 508 | "Tornando a considerare i tre possibili casi di combinazione tra tipo apparente e concreto" 509 | ] 510 | }, 511 | { 512 | "cell_type": "code", 513 | "execution_count": 17, 514 | "metadata": { 515 | "vscode": { 516 | "languageId": "java" 517 | } 518 | }, 519 | "outputs": [], 520 | "source": [ 521 | "Above aa = new Above();\n", 522 | "Above ab = new Below();\n", 523 | "Below bb = new Below();" 524 | ] 525 | }, 526 | { 527 | "cell_type": "markdown", 528 | "metadata": {}, 529 | "source": [ 530 | "è ovvio che su `aa` e `bb` l'invocazione corrisponderà alle implementazioni definite nella classe del tipo (apparente e concreto):" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": 18, 536 | "metadata": { 537 | "vscode": { 538 | "languageId": "java" 539 | } 540 | }, 541 | "outputs": [ 542 | { 543 | "name": "stdout", 544 | "output_type": "stream", 545 | "text": [ 546 | "Above::f(double)\n", 547 | "Below::f(double)\n" 548 | ] 549 | } 550 | ], 551 | "source": [ 552 | "aa.f(1.0);\n", 553 | "bb.f(1.0);" 554 | ] 555 | }, 556 | { 557 | "cell_type": "markdown", 558 | "metadata": {}, 559 | "source": [ 560 | "Il caso interessante è quello in cui il tipo concreto è il sottotipo; in tal caso, una volta che il compilatore ha determinato che la segnatura da chiamare è `f(double)`, l'invocazione riguarderà però il codice presente nell'implementazione del tipo concreto:" 561 | ] 562 | }, 563 | { 564 | "cell_type": "code", 565 | "execution_count": 19, 566 | "metadata": { 567 | "vscode": { 568 | "languageId": "java" 569 | } 570 | }, 571 | "outputs": [ 572 | { 573 | "name": "stdout", 574 | "output_type": "stream", 575 | "text": [ 576 | "Below::f(double)\n" 577 | ] 578 | } 579 | ], 580 | "source": [ 581 | "ab.f(1.0);" 582 | ] 583 | }, 584 | { 585 | "cell_type": "markdown", 586 | "metadata": {}, 587 | "source": [ 588 | "# Overloading ed ereditarietà\n", 589 | "\n", 590 | "Un caso più complesso (e interessante) è quando l'*overloading* è determinato dall'ereditarietà, ossia quando è un metodo del sottotipo a causare l'overloading di uno che è definito nel supertipo." 591 | ] 592 | }, 593 | { 594 | "cell_type": "code", 595 | "execution_count": 20, 596 | "metadata": { 597 | "vscode": { 598 | "languageId": "java" 599 | } 600 | }, 601 | "outputs": [], 602 | "source": [ 603 | "public class Above {\n", 604 | " public void f(double x) {\n", 605 | " System.out.println(\"Above::f(double)\");\n", 606 | " }\n", 607 | "}\n", 608 | "\n", 609 | "public class Below extends Above {\n", 610 | " public void f(int x) {\n", 611 | " System.out.println(\"Below::f(int)\");\n", 612 | " }\n", 613 | "}" 614 | ] 615 | }, 616 | { 617 | "cell_type": "markdown", 618 | "metadata": {}, 619 | "source": [ 620 | "Come nel caso precedente, i casi in cui il tipo apparente coincide con quello concreto e non ci sono conversioni, sono banali:" 621 | ] 622 | }, 623 | { 624 | "cell_type": "code", 625 | "execution_count": 21, 626 | "metadata": { 627 | "vscode": { 628 | "languageId": "java" 629 | } 630 | }, 631 | "outputs": [ 632 | { 633 | "name": "stdout", 634 | "output_type": "stream", 635 | "text": [ 636 | "Above::f(double)\n", 637 | "Below::f(int)\n" 638 | ] 639 | } 640 | ], 641 | "source": [ 642 | "Above aa = new Above();\n", 643 | "Below bb = new Below();\n", 644 | "\n", 645 | "aa.f(1.0);\n", 646 | "bb.f(1);" 647 | ] 648 | }, 649 | { 650 | "cell_type": "markdown", 651 | "metadata": {}, 652 | "source": [ 653 | "Nel caso del supertipo, la chiamata con argomento `int` seleziona l'unico metodo preente (la cui segnatura è compatibile grazie ad una conversione)" 654 | ] 655 | }, 656 | { 657 | "cell_type": "code", 658 | "execution_count": 22, 659 | "metadata": { 660 | "vscode": { 661 | "languageId": "java" 662 | } 663 | }, 664 | "outputs": [ 665 | { 666 | "name": "stdout", 667 | "output_type": "stream", 668 | "text": [ 669 | "Above::f(double)\n" 670 | ] 671 | } 672 | ], 673 | "source": [ 674 | "aa.f(1);" 675 | ] 676 | }, 677 | { 678 | "cell_type": "markdown", 679 | "metadata": {}, 680 | "source": [ 681 | "In quello del sottotipo, la chiamata con argomento `double` seleziona il metodo ereditato (che non richiede conversioni)" 682 | ] 683 | }, 684 | { 685 | "cell_type": "code", 686 | "execution_count": 23, 687 | "metadata": { 688 | "vscode": { 689 | "languageId": "java" 690 | } 691 | }, 692 | "outputs": [ 693 | { 694 | "name": "stdout", 695 | "output_type": "stream", 696 | "text": [ 697 | "Above::f(double)\n" 698 | ] 699 | } 700 | ], 701 | "source": [ 702 | "bb.f(1.0);" 703 | ] 704 | }, 705 | { 706 | "cell_type": "markdown", 707 | "metadata": {}, 708 | "source": [ 709 | "La cosa si fa interessante se il tipo apparente non coincide con quello concreto. In tal caso, non sorprendentemente, la chiamata con argomento `double` seleziona il metodo del tipo apparente con tale segnatura:" 710 | ] 711 | }, 712 | { 713 | "cell_type": "code", 714 | "execution_count": 24, 715 | "metadata": { 716 | "vscode": { 717 | "languageId": "java" 718 | } 719 | }, 720 | "outputs": [ 721 | { 722 | "name": "stdout", 723 | "output_type": "stream", 724 | "text": [ 725 | "Above::f(double)\n" 726 | ] 727 | } 728 | ], 729 | "source": [ 730 | "Above ab = new Below();\n", 731 | "\n", 732 | "ab.f(1.0);" 733 | ] 734 | }, 735 | { 736 | "cell_type": "markdown", 737 | "metadata": {}, 738 | "source": [ 739 | "Cosa succede però con argomento `int`? Dal momento che il sottotipo ha un metodo che non ricihede conversioni, ci si potrebbe attendere che sia esso a venir eseguito." 740 | ] 741 | }, 742 | { 743 | "cell_type": "code", 744 | "execution_count": 25, 745 | "metadata": { 746 | "vscode": { 747 | "languageId": "java" 748 | } 749 | }, 750 | "outputs": [ 751 | { 752 | "name": "stdout", 753 | "output_type": "stream", 754 | "text": [ 755 | "Above::f(double)\n" 756 | ] 757 | } 758 | ], 759 | "source": [ 760 | "ab.f(1);" 761 | ] 762 | }, 763 | { 764 | "cell_type": "markdown", 765 | "metadata": {}, 766 | "source": [ 767 | "Questo però non avviene perché il compilatore seleziona la segnatura sulla base del tipo apparente: per `Above` la segnatura selezionata è `f(double)` che è compatibile grazie ad una conversione. Una volta selezionata la segnatura, l'invocazione utilizzerà l'implementazione di un metodo di tale segnatura nel sottotipo; tale metodo non è definito nel sottotipo, ma è ereditato dal supertipo.\n", 768 | "\n", 769 | "## Overriding e overloading (e ereditarità)\n", 770 | "\n", 771 | "Avendo analizzato separatamente i vari meccanismi che regolano i casi precedenti, non è difficile comprendere un caso in cui si presentino assieme tutte le possibilità." 772 | ] 773 | }, 774 | { 775 | "cell_type": "code", 776 | "execution_count": 26, 777 | "metadata": { 778 | "vscode": { 779 | "languageId": "java" 780 | } 781 | }, 782 | "outputs": [], 783 | "source": [ 784 | "public class Above {\n", 785 | " public void f(double x) {\n", 786 | " System.out.println(\"Above::f(double)\");\n", 787 | " }\n", 788 | "}\n", 789 | "\n", 790 | "public class Below extends Above {\n", 791 | " public void f(int x) {\n", 792 | " System.out.println(\"Below::f(int)\");\n", 793 | " }\n", 794 | " @Override\n", 795 | " public void f(double x) {\n", 796 | " System.out.println(\"Below::f(double)\");\n", 797 | " }\n", 798 | "}" 799 | ] 800 | }, 801 | { 802 | "cell_type": "markdown", 803 | "metadata": {}, 804 | "source": [ 805 | "Restano come sempre i casi banali (tipo apparente coincidente con il concreto, nessuna conversione):" 806 | ] 807 | }, 808 | { 809 | "cell_type": "code", 810 | "execution_count": 27, 811 | "metadata": { 812 | "vscode": { 813 | "languageId": "java" 814 | } 815 | }, 816 | "outputs": [ 817 | { 818 | "name": "stdout", 819 | "output_type": "stream", 820 | "text": [ 821 | "Above::f(double)\n", 822 | "Below::f(int)\n", 823 | "Below::f(double)\n" 824 | ] 825 | } 826 | ], 827 | "source": [ 828 | "Above aa = new Above();\n", 829 | "Below bb = new Below();\n", 830 | "\n", 831 | "aa.f(1.0);\n", 832 | "bb.f(1);\n", 833 | "bb.f(1.0);" 834 | ] 835 | }, 836 | { 837 | "cell_type": "markdown", 838 | "metadata": {}, 839 | "source": [ 840 | "Nel caso del supertipo, l'invocazione col tipo `int` sarà soddisfatta tramite una conversione" 841 | ] 842 | }, 843 | { 844 | "cell_type": "code", 845 | "execution_count": 28, 846 | "metadata": { 847 | "vscode": { 848 | "languageId": "java" 849 | } 850 | }, 851 | "outputs": [ 852 | { 853 | "name": "stdout", 854 | "output_type": "stream", 855 | "text": [ 856 | "Above::f(double)\n" 857 | ] 858 | } 859 | ], 860 | "source": [ 861 | "aa.f(1);" 862 | ] 863 | }, 864 | { 865 | "cell_type": "markdown", 866 | "metadata": {}, 867 | "source": [ 868 | "Nel caso in cui il tipo apparente è diverso dal concreto, quale che sia il tipo dell'argomento, verrà selezionata l'unica segnatura possibile dato il tipo apparente che è `f(double)`. Certamente, una volta selezionata la segnatura, l'invocazione si userà però l'implementazione del sottotipo che fa overriding di quella nel supertipo." 869 | ] 870 | }, 871 | { 872 | "cell_type": "code", 873 | "execution_count": 29, 874 | "metadata": { 875 | "vscode": { 876 | "languageId": "java" 877 | } 878 | }, 879 | "outputs": [ 880 | { 881 | "name": "stdout", 882 | "output_type": "stream", 883 | "text": [ 884 | "Below::f(double)\n", 885 | "Below::f(double)\n" 886 | ] 887 | } 888 | ], 889 | "source": [ 890 | "Above ab = new Below();\n", 891 | "\n", 892 | "ab.f(1);\n", 893 | "ab.f(1.0);" 894 | ] 895 | }, 896 | { 897 | "cell_type": "markdown", 898 | "metadata": {}, 899 | "source": [ 900 | "Su `ab` non c'è quindi verso di ottenere l'esecuzione del metodo definito in `Below` con segnatura `f(int)`, o di alcun metodo di nome `f` definito in `Above`!" 901 | ] 902 | } 903 | ], 904 | "metadata": { 905 | "kernelspec": { 906 | "display_name": "Java", 907 | "language": "java", 908 | "name": "java" 909 | }, 910 | "language_info": { 911 | "codemirror_mode": "java", 912 | "file_extension": ".jshell", 913 | "mimetype": "text/x-java-source", 914 | "name": "Java", 915 | "pygments_lexer": "java", 916 | "version": "21.0.5+11-Ubuntu-1ubuntu124.04" 917 | } 918 | }, 919 | "nbformat": 4, 920 | "nbformat_minor": 2 921 | } 922 | -------------------------------------------------------------------------------- /notes/TGERDS.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "attachments": {}, 5 | "cell_type": "markdown", 6 | "metadata": {}, 7 | "source": [ 8 | "# Tipi generici e relazioni di sottotipo\n", 9 | "\n", 10 | "Se `S` $\\prec$ `T` non è vero che ≠ `G` $\\prec$ `G` (dove `G` è un qualche tipo generico). Usando una immagine tratta dal [tutorial](https://dev.java/learn/generics/intro/)\n", 11 | "\n", 12 | "
\n", 13 | "\n", 14 | "Si considerino ad esempio" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "vscode": { 22 | "languageId": "java" 23 | } 24 | }, 25 | "outputs": [], 26 | "source": [ 27 | "List li = new ArrayList<>();\n", 28 | "List lo;" 29 | ] 30 | }, 31 | { 32 | "attachments": {}, 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "Se `li` fosse un sottotipo di `lo` l'assegnamento seguente sarebbe possibile (senza *cast*) " 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 2, 42 | "metadata": { 43 | "vscode": { 44 | "languageId": "java" 45 | } 46 | }, 47 | "outputs": [], 48 | "source": [ 49 | "lo = (List)li; " 50 | ] 51 | }, 52 | { 53 | "attachments": {}, 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "Una volta ottenuto l'*alias* `lo` potremmo usarlo per aggiungere di tutto a `li`" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 3, 63 | "metadata": { 64 | "vscode": { 65 | "languageId": "java" 66 | } 67 | }, 68 | "outputs": [ 69 | { 70 | "data": { 71 | "text/plain": [ 72 | "true" 73 | ] 74 | }, 75 | "execution_count": 3, 76 | "metadata": {}, 77 | "output_type": "execute_result" 78 | } 79 | ], 80 | "source": [ 81 | "lo.add(new Object());" 82 | ] 83 | }, 84 | { 85 | "attachments": {}, 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "ma ovviamente, una volta estratto da `li` (come `Integer`) un oggetto qualunque potrebbe dare adito ad un errore di conversione" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 4, 95 | "metadata": { 96 | "vscode": { 97 | "languageId": "java" 98 | } 99 | }, 100 | "outputs": [ 101 | { 102 | "ename": "EvalException", 103 | "evalue": "class java.lang.Object cannot be cast to class java.lang.Integer (java.lang.Object and java.lang.Integer are in module java.base of loader 'bootstrap')", 104 | "output_type": "error", 105 | "traceback": [ 106 | "\u001b[1m\u001b[31m---------------------------------------------------------------------------\u001b[0m", 107 | "\u001b[1m\u001b[31mjava.lang.ClassCastException: class java.lang.Object cannot be cast to class java.lang.Integer (java.lang.Object and java.lang.Integer are in module java.base of loader 'bootstrap')\u001b[0m", 108 | "\u001b[1m\u001b[31m\tat .(#16:1)\u001b[0m" 109 | ] 110 | } 111 | ], 112 | "source": [ 113 | "li.get(0);" 114 | ] 115 | }, 116 | { 117 | "attachments": {}, 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "Abbiamo già visto che il punto, in questo caso, è poter aggiungere e togliere elementi del tipo parametrico (avendo sia la garnzia della *type safety* che evitando l'uso del *cast*)." 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 5, 127 | "metadata": { 128 | "vscode": { 129 | "languageId": "java" 130 | } 131 | }, 132 | "outputs": [], 133 | "source": [ 134 | "li.add(Integer.valueOf(1));\n", 135 | "Integer i = li.get(1);" 136 | ] 137 | }, 138 | { 139 | "attachments": {}, 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "Inoltre, come già discusso, vale invece che se `G` $\\prec$ `H`, allora per ogni `T` vale che `G` $\\prec$ `H`, di nuovo usando una immagine dal [tutorial](https://dev.java/learn/generics/intro/)\n", 144 | "\n", 145 | "
" 146 | ] 147 | }, 148 | { 149 | "attachments": {}, 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "## Array e gerarchia\n", 154 | "\n", 155 | "Si osservi che la situazione è ben **diversa** per gli array: è infatti vero che se `S` $\\prec$ `T`, allora `S[]` $\\prec$ `T[]`." 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 6, 161 | "metadata": { 162 | "vscode": { 163 | "languageId": "java" 164 | } 165 | }, 166 | "outputs": [], 167 | "source": [ 168 | "Integer[] ai = new Integer[10];\n", 169 | "Object[] ao;" 170 | ] 171 | }, 172 | { 173 | "attachments": {}, 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "In questo caso, infatti, l'assegmaneto è possibile senza *cast* (per via della relazione di sottotipo)" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 7, 183 | "metadata": { 184 | "vscode": { 185 | "languageId": "java" 186 | } 187 | }, 188 | "outputs": [], 189 | "source": [ 190 | "ao = ai;" 191 | ] 192 | }, 193 | { 194 | "attachments": {}, 195 | "cell_type": "markdown", 196 | "metadata": {}, 197 | "source": [ 198 | "Il fatto è che, per gli array, l'assegnamento a `ao` può essere controllato a *run-time* perché l'array conserva l'informazione circa il tipo dei suoi elementi (cosa che non accade per i tipi generici)." 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 8, 204 | "metadata": { 205 | "vscode": { 206 | "languageId": "java" 207 | } 208 | }, 209 | "outputs": [ 210 | { 211 | "ename": "EvalException", 212 | "evalue": "java.lang.Object", 213 | "output_type": "error", 214 | "traceback": [ 215 | "\u001b[1m\u001b[31m---------------------------------------------------------------------------\u001b[0m", 216 | "\u001b[1m\u001b[31mjava.lang.ArrayStoreException: java.lang.Object\u001b[0m", 217 | "\u001b[1m\u001b[31m\tat .(#22:1)\u001b[0m" 218 | ] 219 | } 220 | ], 221 | "source": [ 222 | "ao[0] = new Object();" 223 | ] 224 | }, 225 | { 226 | "attachments": {}, 227 | "cell_type": "markdown", 228 | "metadata": {}, 229 | "source": [ 230 | "L'eccezione `ArrayStoreException` è proprio il modo in cui la VM segnala (a runtime) che è impossibile assegnare un oggetto ad un elemento di `as` (tramite l'*alias* `ao`).\n", 231 | "\n", 232 | "Ovviamente è corretto l'assegnamento" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 9, 238 | "metadata": { 239 | "vscode": { 240 | "languageId": "java" 241 | } 242 | }, 243 | "outputs": [ 244 | { 245 | "data": { 246 | "text/plain": [ 247 | "1" 248 | ] 249 | }, 250 | "execution_count": 9, 251 | "metadata": {}, 252 | "output_type": "execute_result" 253 | } 254 | ], 255 | "source": [ 256 | "ao[0] = Integer.valueOf(1);" 257 | ] 258 | }, 259 | { 260 | "attachments": {}, 261 | "cell_type": "markdown", 262 | "metadata": {}, 263 | "source": [ 264 | "che viene eseguito senza errore.\n", 265 | " \n", 266 | "Nel caso degli array quindi la *type safety* può essee garantita anche \"trasportando\" sugli array la relazione di sottotipo dgli elementi." 267 | ] 268 | }, 269 | { 270 | "attachments": {}, 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "# Metodi generici\n", 275 | "\n", 276 | "La mancanza di relazioni di tipo tra `G` e `G` anche qualora `S` $\\prec$ `T` è particolarmente grave non tanto per il caso degli *alias* visto in precendeza, quanto per il caso dei metodi.\n", 277 | "\n", 278 | "Supponiamo di voler scrivere il metodo `add` al quale passeremo un parametro che \"produca\" dei valori numerici di cui il metodo restiturà la somma" 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": 10, 284 | "metadata": { 285 | "vscode": { 286 | "languageId": "java" 287 | } 288 | }, 289 | "outputs": [], 290 | "source": [ 291 | "public static double add(List lst) {\n", 292 | " double sum = 0;\n", 293 | " for (Number n : lst) sum += n.doubleValue();\n", 294 | " return sum;\n", 295 | "}" 296 | ] 297 | }, 298 | { 299 | "attachments": {}, 300 | "cell_type": "markdown", 301 | "metadata": {}, 302 | "source": [ 303 | "Tutto bene se lo usiamo con una lista del tipo del parametro" 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": 11, 309 | "metadata": { 310 | "vscode": { 311 | "languageId": "java" 312 | } 313 | }, 314 | "outputs": [ 315 | { 316 | "data": { 317 | "text/plain": [ 318 | "6.5" 319 | ] 320 | }, 321 | "execution_count": 11, 322 | "metadata": {}, 323 | "output_type": "execute_result" 324 | } 325 | ], 326 | "source": [ 327 | "List nums = List.of(1, 2.5, 3);\n", 328 | "add(nums)" 329 | ] 330 | }, 331 | { 332 | "attachments": {}, 333 | "cell_type": "markdown", 334 | "metadata": {}, 335 | "source": [ 336 | "Ma per via della mancanza della relazione di sottotipo, non possiamo usarla per sommare una lista di interi" 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": 12, 342 | "metadata": { 343 | "vscode": { 344 | "languageId": "java" 345 | } 346 | }, 347 | "outputs": [ 348 | { 349 | "ename": "CompilationException", 350 | "evalue": "", 351 | "output_type": "error", 352 | "traceback": [ 353 | "\u001b[1m\u001b[30m| \u001b[1m\u001b[30madd(\u001b[0m\u001b[1m\u001b[30m\u001b[41mints\u001b[0m\u001b[1m\u001b[30m);\u001b[0m", 354 | "\u001b[1m\u001b[31mincompatible types: java.util.List cannot be converted to java.util.List\u001b[0m", 355 | "" 356 | ] 357 | } 358 | ], 359 | "source": [ 360 | "List ints = List.of(1, 2, 3);\n", 361 | "add(ints);" 362 | ] 363 | }, 364 | { 365 | "attachments": {}, 366 | "cell_type": "markdown", 367 | "metadata": {}, 368 | "source": [ 369 | "Una possibile soluzione è rendere il metodo **generico** ed indicare un **bound** nella dichiarazione dei parametri di tipo" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": 13, 375 | "metadata": { 376 | "vscode": { 377 | "languageId": "java" 378 | } 379 | }, 380 | "outputs": [], 381 | "source": [ 382 | "public static double add(List lst) {\n", 383 | " double sum = 0;\n", 384 | " for (T n : lst) sum += n.doubleValue();\n", 385 | " return sum;\n", 386 | "}" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 14, 392 | "metadata": { 393 | "vscode": { 394 | "languageId": "java" 395 | } 396 | }, 397 | "outputs": [ 398 | { 399 | "data": { 400 | "text/plain": [ 401 | "6.0" 402 | ] 403 | }, 404 | "execution_count": 14, 405 | "metadata": {}, 406 | "output_type": "execute_result" 407 | } 408 | ], 409 | "source": [ 410 | "add(ints);" 411 | ] 412 | }, 413 | { 414 | "attachments": {}, 415 | "cell_type": "markdown", 416 | "metadata": {}, 417 | "source": [ 418 | "Supponiamo ora di voler scrivere un metodo `copy` che data una lista proceda a copiare il suo contenuto in un \"consumatore\" costituito da una seconda lista" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": 15, 424 | "metadata": { 425 | "vscode": { 426 | "languageId": "java" 427 | } 428 | }, 429 | "outputs": [], 430 | "source": [ 431 | "public static void copy(List src, List dst) {\n", 432 | " dst.clear();\n", 433 | " for (T t : src) dst.add(t);\n", 434 | "}" 435 | ] 436 | }, 437 | { 438 | "attachments": {}, 439 | "cell_type": "markdown", 440 | "metadata": {}, 441 | "source": [ 442 | "che funziona egregiamente su coppie di liste di interi" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 16, 448 | "metadata": { 449 | "vscode": { 450 | "languageId": "java" 451 | } 452 | }, 453 | "outputs": [ 454 | { 455 | "data": { 456 | "text/plain": [ 457 | "[1, 2, 3]" 458 | ] 459 | }, 460 | "execution_count": 16, 461 | "metadata": {}, 462 | "output_type": "execute_result" 463 | } 464 | ], 465 | "source": [ 466 | "List dupInts = new ArrayList<>();\n", 467 | "copy(ints, dupInts);\n", 468 | "dupInts" 469 | ] 470 | }, 471 | { 472 | "attachments": {}, 473 | "cell_type": "markdown", 474 | "metadata": {}, 475 | "source": [ 476 | "Ma che succede se volessimo copiare una lista di interi in una di numeri?" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 17, 482 | "metadata": { 483 | "vscode": { 484 | "languageId": "java" 485 | } 486 | }, 487 | "outputs": [ 488 | { 489 | "ename": "CompilationException", 490 | "evalue": "", 491 | "output_type": "error", 492 | "traceback": [ 493 | "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[0m\u001b[1m\u001b[30m\u001b[41mcopy\u001b[0m\u001b[1m\u001b[30m(ints, dupNums);\u001b[0m", 494 | "\u001b[1m\u001b[31mmethod copy in class cannot be applied to given types;\u001b[0m", 495 | "\u001b[1m\u001b[31m required: java.util.List,java.util.List\u001b[0m", 496 | "\u001b[1m\u001b[31m found: java.util.List,java.util.List\u001b[0m", 497 | "\u001b[1m\u001b[31m reason: inference variable T has incompatible equality constraints java.lang.Number,java.lang.Integer\u001b[0m", 498 | "" 499 | ] 500 | } 501 | ], 502 | "source": [ 503 | "List dupNums = new ArrayList<>();\n", 504 | "copy(ints, dupNums);" 505 | ] 506 | }, 507 | { 508 | "attachments": {}, 509 | "cell_type": "markdown", 510 | "metadata": {}, 511 | "source": [ 512 | "Incorreremmo di nuovo in un problema legato all'assenza di una relazione gerarchica tra i tipi; anche in questo caso, possiamo risolverlo con un *bound*" 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "execution_count": 18, 518 | "metadata": { 519 | "vscode": { 520 | "languageId": "java" 521 | } 522 | }, 523 | "outputs": [], 524 | "source": [ 525 | "public static void copy(List src, List dst) {\n", 526 | " dst.clear();\n", 527 | " for (S t : src) dst.add(t);\n", 528 | "}" 529 | ] 530 | }, 531 | { 532 | "cell_type": "code", 533 | "execution_count": 19, 534 | "metadata": { 535 | "vscode": { 536 | "languageId": "java" 537 | } 538 | }, 539 | "outputs": [ 540 | { 541 | "data": { 542 | "text/plain": [ 543 | "[1, 2, 3]" 544 | ] 545 | }, 546 | "execution_count": 19, 547 | "metadata": {}, 548 | "output_type": "execute_result" 549 | } 550 | ], 551 | "source": [ 552 | "copy(ints, dupNums);\n", 553 | "dupNums" 554 | ] 555 | }, 556 | { 557 | "attachments": {}, 558 | "cell_type": "markdown", 559 | "metadata": {}, 560 | "source": [ 561 | "L'uso dei bound sui tipi di parametro dei metodi generici è una prima risposta al problema della mancanza di una gerarchia tra i generici.\n", 562 | "\n", 563 | "Ma è necessaria una soluzione più versatile, in grado di permetterci di dare un tipo (generico) sensato agli argomenti dei metodi." 564 | ] 565 | }, 566 | { 567 | "attachments": {}, 568 | "cell_type": "markdown", 569 | "metadata": {}, 570 | "source": [ 571 | "# Wildcard\n", 572 | "\n", 573 | "Esiste un parametro di tipo **wildcard** `?` che intuitivamente sta per \"qualunque tipo\", ragion per cui vale `G` $\\prec$`G` (per ogni `T`); con un immagine tratta dal [tutorial](https://dev.java/learn/generics/wildcards/)\n", 574 | "\n", 575 | "
\n", 576 | "\n", 577 | "Ad esempio" 578 | ] 579 | }, 580 | { 581 | "cell_type": "code", 582 | "execution_count": 20, 583 | "metadata": { 584 | "vscode": { 585 | "languageId": "java" 586 | } 587 | }, 588 | "outputs": [], 589 | "source": [ 590 | "List lw;\n", 591 | "\n", 592 | "lw = lo;\n", 593 | "lw = li;" 594 | ] 595 | }, 596 | { 597 | "attachments": {}, 598 | "cell_type": "markdown", 599 | "metadata": {}, 600 | "source": [ 601 | "Il problema è che in questo modo non è possibile garantire alcuna *type safety*, infatti non c'è verso di scrivere nella lista" 602 | ] 603 | }, 604 | { 605 | "cell_type": "code", 606 | "execution_count": 21, 607 | "metadata": { 608 | "vscode": { 609 | "languageId": "java" 610 | } 611 | }, 612 | "outputs": [ 613 | { 614 | "ename": "CompilationException", 615 | "evalue": "", 616 | "output_type": "error", 617 | "traceback": [ 618 | "\u001b[1m\u001b[30m| \u001b[1m\u001b[30mlw.add(\u001b[0m\u001b[1m\u001b[30m\u001b[41mInteger.valueOf(1)\u001b[0m\u001b[1m\u001b[30m);\u001b[0m", 619 | "\u001b[1m\u001b[31mincompatible types: java.lang.Integer cannot be converted to capture#2 of ?\u001b[0m", 620 | "" 621 | ] 622 | } 623 | ], 624 | "source": [ 625 | "lw.add(Integer.valueOf(1));" 626 | ] 627 | }, 628 | { 629 | "cell_type": "code", 630 | "execution_count": 22, 631 | "metadata": { 632 | "vscode": { 633 | "languageId": "java" 634 | } 635 | }, 636 | "outputs": [ 637 | { 638 | "ename": "CompilationException", 639 | "evalue": "", 640 | "output_type": "error", 641 | "traceback": [ 642 | "\u001b[1m\u001b[30m| \u001b[1m\u001b[30mlw.add(\u001b[0m\u001b[1m\u001b[30m\u001b[41mnew Object()\u001b[0m\u001b[1m\u001b[30m);\u001b[0m", 643 | "\u001b[1m\u001b[31mincompatible types: java.lang.Object cannot be converted to capture#4 of ?\u001b[0m", 644 | "" 645 | ] 646 | } 647 | ], 648 | "source": [ 649 | "lw.add(new Object());" 650 | ] 651 | }, 652 | { 653 | "attachments": {}, 654 | "cell_type": "markdown", 655 | "metadata": {}, 656 | "source": [ 657 | "Anche la lettura non può essere fatta in modo *type safe*" 658 | ] 659 | }, 660 | { 661 | "cell_type": "code", 662 | "execution_count": 23, 663 | "metadata": { 664 | "vscode": { 665 | "languageId": "java" 666 | } 667 | }, 668 | "outputs": [ 669 | { 670 | "ename": "CompilationException", 671 | "evalue": "", 672 | "output_type": "error", 673 | "traceback": [ 674 | "\u001b[1m\u001b[30m| \u001b[1m\u001b[30mInteger i = \u001b[0m\u001b[1m\u001b[30m\u001b[41mlw.get(1)\u001b[0m\u001b[1m\u001b[30m;\u001b[0m", 675 | "\u001b[1m\u001b[31mincompatible types: capture#5 of ? cannot be converted to java.lang.Integer\u001b[0m", 676 | "" 677 | ] 678 | } 679 | ], 680 | "source": [ 681 | "Integer i = lw.get(1);" 682 | ] 683 | }, 684 | { 685 | "attachments": {}, 686 | "cell_type": "markdown", 687 | "metadata": {}, 688 | "source": [ 689 | "Ma se l'obiettivo è solo recuperare un elemento, si può fare a patto di usare `Object` come tipo d'ultima istanza" 690 | ] 691 | }, 692 | { 693 | "cell_type": "code", 694 | "execution_count": 24, 695 | "metadata": { 696 | "vscode": { 697 | "languageId": "java" 698 | } 699 | }, 700 | "outputs": [], 701 | "source": [ 702 | "Object o = lw.get(0);" 703 | ] 704 | }, 705 | { 706 | "attachments": {}, 707 | "cell_type": "markdown", 708 | "metadata": {}, 709 | "source": [ 710 | "Tale \"libertà\" può essere vincolata in vario modo, così che abbia senso usare tipi parametrici basati su wildcard.\n", 711 | "\n", 712 | "## Upper bound\n", 713 | "\n", 714 | "Il caso più semplice è quello degli **upper bound** della forma `? extends T` che consentono di introdurre le seguenti relazione di sottotipo (fissato `G`):\n", 715 | "\n", 716 | "* per ogni `T`, `G` $\\prec$ `G` \n", 717 | "* se `S` $\\prec$ `T`, allora `G` $\\prec$ `G`.\n", 718 | "\n", 719 | "Ragionando per transitività, se `S` $\\prec$ `T`, si ha `G` $\\prec$ `G` che, in somma, permette di concludere che il generico basato sull'*upper bound del supertipo* (`G`) è supertipo sia di `G` che di `G` (che pure sono tra loro inconfrontabili dal punto di vista della gerarchia).\n", 720 | "\n", 721 | "### Esempio: produttore\n", 722 | "\n", 723 | "Un esempio d'uso può chiarire l'obiettivo di tali bound. Immaginiamo di avere un metodo in grado di operare su oggetti di tipo `T` prodotti da una lista; esso potrà riceverne una di tipo `List` ma, certamente, anche `List` (se `S` $\\prec$ `T`); per questa ragione ha senso che il tipo del sua parametro sia `List`.\n", 724 | "\n", 725 | "Ad esempio, consideriamo il metodo `add` visto in precedenza: a questo punto ha senso abbia un parametro di tipo `List` che è supertipo di `List` e `List`." 726 | ] 727 | }, 728 | { 729 | "cell_type": "code", 730 | "execution_count": 25, 731 | "metadata": { 732 | "vscode": { 733 | "languageId": "java" 734 | } 735 | }, 736 | "outputs": [], 737 | "source": [ 738 | "static double add(List lst) {\n", 739 | " double sum = 0;\n", 740 | " for (Number n : lst) sum += n.doubleValue();\n", 741 | " return sum;\n", 742 | "}" 743 | ] 744 | }, 745 | { 746 | "cell_type": "code", 747 | "execution_count": 26, 748 | "metadata": { 749 | "vscode": { 750 | "languageId": "java" 751 | } 752 | }, 753 | "outputs": [ 754 | { 755 | "data": { 756 | "text/plain": [ 757 | "6.0" 758 | ] 759 | }, 760 | "execution_count": 26, 761 | "metadata": {}, 762 | "output_type": "execute_result" 763 | } 764 | ], 765 | "source": [ 766 | "add(ints);" 767 | ] 768 | }, 769 | { 770 | "cell_type": "code", 771 | "execution_count": 27, 772 | "metadata": { 773 | "vscode": { 774 | "languageId": "java" 775 | } 776 | }, 777 | "outputs": [ 778 | { 779 | "data": { 780 | "text/plain": [ 781 | "6.5" 782 | ] 783 | }, 784 | "execution_count": 27, 785 | "metadata": {}, 786 | "output_type": "execute_result" 787 | } 788 | ], 789 | "source": [ 790 | "add(nums);" 791 | ] 792 | }, 793 | { 794 | "attachments": {}, 795 | "cell_type": "markdown", 796 | "metadata": {}, 797 | "source": [ 798 | "Il parametro del metodo `add` viene chiamato **produttore** perché produce i valori adoperati dal metodo, se esso è in grado di gestire il tipo `Number` sarà in grado di gestire i sottotipi. Il produttore, quindi, deve emettere elementi *al più* di un certo tipo, per questa ragione il suo parametro ha un *upper bound*.\n", 799 | "\n", 800 | "## Lower bound\n", 801 | "\n", 802 | "Immaginiamo ora di avere un metodo in grado di consumare oggetti di tipo `T` da immagazzinare in una lista, che tipo dovrebbe avere quest'ultima? Non possiamo seguire il ragionamento precedente: volendo aggiungere valori di tipo `T` non possiamo farlo in una lista di tipo `List`, perché sappiamo che se `S` $\\prec$ `T` ad essa può corrispondere anche una `List` e finiremmo col mettere oggetti del supertipo in una lista di sottotipi! Vorremmo scrivere una cosa del tipo `T extends ?`, ma questo non è sintatticamente ammesso.\n", 803 | "\n", 804 | "A tal fine vengono invece introdotti i **lower bound** della forma `? super S` che consentono di introdurre le seguenti relazioni di sottotipo\n", 805 | "\n", 806 | "* per ogni `T`, `G` $\\prec$ `G` \n", 807 | "* se `S` $\\prec$ `T`, allora `G` $\\prec$ `G`\n", 808 | "\n", 809 | "si osservi che, nella seconda relazione, l'ordine dei generici è rovesciato rispetto a prima.\n", 810 | "\n", 811 | "Ragionando per transitività, se `S` $\\prec$ `T`, si ha `G` $\\prec$ `G` che, in somma, permette di concludere che: il generico basato sul *lower bound del sottotipo* (`G`) è supertipo sia di `G` che di `G`.\n", 812 | "\n", 813 | "### Esempio: consumatore\n", 814 | "\n", 815 | "Ad esempio, consideriamo di nuovo il metodo `copy` visto in precedenza. L'unico vincolo in questo caso è che il consumatore di elementi (estratti dalla lista di `T`) sia una `List` (che è supertipo di `List`) in grado di ricevere elementi di tipo `T`." 816 | ] 817 | }, 818 | { 819 | "cell_type": "code", 820 | "execution_count": 28, 821 | "metadata": { 822 | "vscode": { 823 | "languageId": "java" 824 | } 825 | }, 826 | "outputs": [], 827 | "source": [ 828 | "static void copy(List src, List dst) {\n", 829 | " dst.clear();\n", 830 | " for (T t : src) dst.add(t);\n", 831 | "}" 832 | ] 833 | }, 834 | { 835 | "cell_type": "code", 836 | "execution_count": 29, 837 | "metadata": { 838 | "vscode": { 839 | "languageId": "java" 840 | } 841 | }, 842 | "outputs": [ 843 | { 844 | "data": { 845 | "text/plain": [ 846 | "[1, 2, 3]" 847 | ] 848 | }, 849 | "execution_count": 29, 850 | "metadata": {}, 851 | "output_type": "execute_result" 852 | } 853 | ], 854 | "source": [ 855 | "copy(ints, dupInts);\n", 856 | "dupInts" 857 | ] 858 | }, 859 | { 860 | "cell_type": "code", 861 | "execution_count": 30, 862 | "metadata": { 863 | "vscode": { 864 | "languageId": "java" 865 | } 866 | }, 867 | "outputs": [ 868 | { 869 | "data": { 870 | "text/plain": [ 871 | "[1, 2, 3]" 872 | ] 873 | }, 874 | "execution_count": 30, 875 | "metadata": {}, 876 | "output_type": "execute_result" 877 | } 878 | ], 879 | "source": [ 880 | "copy(ints, dupNums);\n", 881 | "dupNums" 882 | ] 883 | }, 884 | { 885 | "attachments": {}, 886 | "cell_type": "markdown", 887 | "metadata": {}, 888 | "source": [ 889 | "Il secondo parametro del metodo `copy` viene chiamato **consumatore** perché riceve o valori dalla (prima) lista." 890 | ] 891 | }, 892 | { 893 | "attachments": {}, 894 | "cell_type": "markdown", 895 | "metadata": {}, 896 | "source": [ 897 | "Si osservi inoltre che con le *wildcard* è possibile anche restringere il tipo della prima lista, ad esempio ad `Integer`, in questo modo non è nemmeno necessario che il metodo sia generico" 898 | ] 899 | }, 900 | { 901 | "cell_type": "code", 902 | "execution_count": 31, 903 | "metadata": { 904 | "vscode": { 905 | "languageId": "java" 906 | } 907 | }, 908 | "outputs": [], 909 | "source": [ 910 | "static void copy(List src, List dst) {\n", 911 | " dst.clear();\n", 912 | " for (Integer t : src) dst.add(t);\n", 913 | "}" 914 | ] 915 | }, 916 | { 917 | "cell_type": "code", 918 | "execution_count": 32, 919 | "metadata": { 920 | "vscode": { 921 | "languageId": "java" 922 | } 923 | }, 924 | "outputs": [ 925 | { 926 | "data": { 927 | "text/plain": [ 928 | "[1, 2, 3]" 929 | ] 930 | }, 931 | "execution_count": 32, 932 | "metadata": {}, 933 | "output_type": "execute_result" 934 | } 935 | ], 936 | "source": [ 937 | "copy(ints, dupInts);\n", 938 | "dupInts" 939 | ] 940 | }, 941 | { 942 | "cell_type": "code", 943 | "execution_count": 33, 944 | "metadata": { 945 | "vscode": { 946 | "languageId": "java" 947 | } 948 | }, 949 | "outputs": [ 950 | { 951 | "data": { 952 | "text/plain": [ 953 | "[1, 2, 3]" 954 | ] 955 | }, 956 | "execution_count": 33, 957 | "metadata": {}, 958 | "output_type": "execute_result" 959 | } 960 | ], 961 | "source": [ 962 | "copy(ints, dupNums);\n", 963 | "dupNums" 964 | ] 965 | }, 966 | { 967 | "attachments": {}, 968 | "cell_type": "markdown", 969 | "metadata": {}, 970 | "source": [ 971 | "## Schemi riassuntivi \n", 972 | "\n", 973 | "Alcune visualizzazioni possono essere comode per ricordare le relazioni introdotte in questa nota. La prima riprende esattamente le relazioni espresse nella precedente sezione\n", 974 | "\n", 975 | "
\n", 976 | "\n", 977 | "Una bella immagine di [Andrey Tyukin](https://stackoverflow.com/users/2707792/andrey-tyukin) può aiutare a riflettere sulle relazioni tra tipo e le nozioni di produttore e consumatore.\n", 978 | "\n", 979 | "
\n", 980 | "\n", 981 | "Per finire, una immagine tratta dal [tutorial](https://dev.java/learn/generics/wildcards/) può aiutare a ricordare l'ordine indotto da queste relazioni\n", 982 | "\n", 983 | "
" 984 | ] 985 | }, 986 | { 987 | "cell_type": "markdown", 988 | "metadata": {}, 989 | "source": [ 990 | "
\n",
 991 |     "digraph G {\n",
 992 |     "\n",
 993 |     "    rankdir=\"BT\";\n",
 994 |     "    node [shape=\"rect\"];\n",
 995 |     "\n",
 996 |     "    gS[label=\"G\"];\n",
 997 |     "    gT[label=\"G\"]; \n",
 998 |     "    gsT[label=\"G\"];\n",
 999 |     "    gsS[label=\"G\"];\n",
1000 |     "    geT[label=\"G\"]; \n",
1001 |     "    geS[label=\"G\"];\n",
1002 |     "    \n",
1003 |     "    gS->gT [style=\"dotted\"];\n",
1004 |     "    \n",
1005 |     "    gS -> geS;\n",
1006 |     "    gT -> geT;\n",
1007 |     "    \n",
1008 |     "    gS -> gsS;\n",
1009 |     "    gT -> gsT;\n",
1010 |     "    \n",
1011 |     "    geS -> geT;\n",
1012 |     "    gsT -> gsS;\n",
1013 |     "    \n",
1014 |     "  }\n",
1015 |     "
" 1016 | ] 1017 | } 1018 | ], 1019 | "metadata": { 1020 | "kernelspec": { 1021 | "display_name": "Java", 1022 | "language": "java", 1023 | "name": "java" 1024 | }, 1025 | "language_info": { 1026 | "codemirror_mode": "java", 1027 | "file_extension": ".jshell", 1028 | "mimetype": "text/x-java-source", 1029 | "name": "Java", 1030 | "pygments_lexer": "java", 1031 | "version": "21.0.5+11-Ubuntu-1ubuntu124.04" 1032 | } 1033 | }, 1034 | "nbformat": 4, 1035 | "nbformat_minor": 2 1036 | } 1037 | -------------------------------------------------------------------------------- /notes/notes.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from textwrap import dedent 3 | 4 | from pygments import highlight as pygments_highlight 5 | from pygments.lexers import get_lexer_for_filename 6 | from pygments.formatters import HtmlFormatter 7 | 8 | from IPython.display import HTML 9 | 10 | CURRENT_BRANCH = 'master' 11 | BASE_DIR = '../src/main/java/' 12 | BASE_URL = f'https://github.com/prog2-unimi/notes/blob/{CURRENT_BRANCH}/src/main/java/' 13 | 14 | def find(fragment, lines): 15 | oc = 0 16 | first = -1 17 | for n, line in enumerate(lines, 1): 18 | if fragment in line: first = n 19 | if first != -1: 20 | oc += line.count('{') - line.count('}') 21 | if oc == 0: return first, 1 + n 22 | return None, None 23 | 24 | def show(path, linenos = False, first = None, last = None, highlight = None, fragment = None): 25 | code = (Path(BASE_DIR) / path).read_text() 26 | lines = code.splitlines() 27 | if fragment: first, last = find(fragment, lines) 28 | first = 0 if first is None else first - 1 29 | last = len(lines) if last is None else last - 1 30 | url = BASE_URL + path 31 | if first > 0 or last < len(lines): 32 | url += '#L{}-L{}'.format(first + 1, last) 33 | code = '\n'.join(code.splitlines()[first:last]) 34 | if isinstance(highlight, list): 35 | highlight = [l - first for l in highlight] 36 | elif isinstance(highlight, str): 37 | hf, hl = find(highlight, lines) 38 | highlight = list(range(hf - first, hl - first)) 39 | lexer = get_lexer_for_filename(path, stripall = False) 40 | formatter = HtmlFormatter( 41 | linenos = 'inline' if linenos else None, 42 | linenostart = 1 if first is None else 1 + first, 43 | cssclass = 'output_html', 44 | nobackground = False, 45 | hl_lines = highlight if highlight else [] 46 | ) 47 | return HTML('{}

[sorgente]'.format( 48 | formatter.get_style_defs('.output_html'), 49 | pygments_highlight(dedent(code), lexer, formatter), 50 | url 51 | )) -------------------------------------------------------------------------------- /notes/tgerds0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prog2-unimi/notes/e62ff5bf29b6bf2c4401b6e59970f9ecfab4f298/notes/tgerds0.png -------------------------------------------------------------------------------- /notes/tgerds1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prog2-unimi/notes/e62ff5bf29b6bf2c4401b6e59970f9ecfab4f298/notes/tgerds1.png -------------------------------------------------------------------------------- /publish: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ./gradlew spotlessApply build 4 | mkdir -p ./docs 5 | for g in notes/*.ipynb; do 6 | note=$(basename $g .ipynb) 7 | if [ $note = Test ]; then continue; fi 8 | echo "Converting $note..." 9 | if grep -q '"mimetype": "text/x-java-source"' "./notes/${note}.ipynb"; then 10 | jupyter nbconvert --execute --ExecutePreprocessor.allow_errors=True --to html "./notes/${note}.ipynb" --template classic 11 | else 12 | jupyter nbconvert --execute --no-prompt --no-input --to html "./notes/${note}.ipynb" --template classic 13 | fi 14 | sed -i -e 's|||' "./notes/${note}.html" 15 | mv -f "./notes/${note}.html" ./docs 16 | done -------------------------------------------------------------------------------- /src/licenseHeaderFile.txt: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/ced/MainECC.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.ced; 23 | 24 | import it.unimi.di.prog2.notes.ced.ec.Adder; 25 | import it.unimi.di.prog2.notes.ced.ec.LogAdderComp; 26 | import java.util.List; 27 | 28 | public class MainECC { 29 | public static void main(String[] args) { 30 | Adder a = new Adder(); 31 | a.add(List.of(1, 2, 3)); 32 | System.out.println(a.result()); 33 | 34 | LogAdderComp lac = new LogAdderComp(); 35 | lac.add(List.of(1, 2, 3)); 36 | System.out.println(lac.result()); 37 | System.out.println(lac.log()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/ced/MainECE.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.ced; 23 | 24 | import it.unimi.di.prog2.notes.ced.ec.Adder; 25 | import it.unimi.di.prog2.notes.ced.ec.LogAdderExt; 26 | import java.util.List; 27 | 28 | public class MainECE { 29 | public static void main(String[] args) { 30 | Adder a = new Adder(); 31 | a.add(List.of(1, 2, 3)); 32 | System.out.println(a.result()); 33 | 34 | LogAdderExt lae = new LogAdderExt(); 35 | lae.add(List.of(1, 2, 3)); 36 | System.out.println(lae.result()); 37 | System.out.println(lae.log()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/ced/MainEF.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.ced; 23 | 24 | import it.unimi.di.prog2.notes.ced.ef.Adder; 25 | import it.unimi.di.prog2.notes.ced.ef.AdderInterface; 26 | import it.unimi.di.prog2.notes.ced.ef.LogAdder; 27 | import java.util.List; 28 | 29 | public class MainEF { 30 | 31 | public static void use(AdderInterface a) { 32 | a.add(List.of(1, 2, 3)); 33 | System.out.println(a.result()); 34 | } 35 | 36 | public static void main(String[] args) { 37 | AdderInterface a = new Adder(); 38 | use(a); 39 | 40 | LogAdder la = new LogAdder(new Adder()); 41 | use(la); 42 | System.out.println(la.log()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/ced/ec/Adder.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.ced.ec; 23 | 24 | import java.util.List; 25 | 26 | public class Adder { 27 | 28 | private int result = 0; 29 | 30 | public void add(int x) { 31 | result += x; 32 | } 33 | 34 | public void add(List l) { 35 | for (int x : l) add(x); 36 | } 37 | 38 | public int result() { 39 | return result; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/ced/ec/LogAdderComp.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.ced.ec; 23 | 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | public class LogAdderComp { 28 | private final Adder adder = new Adder(); 29 | private final List seen = new ArrayList<>(); 30 | 31 | public void add(int x) { 32 | seen.add(x); 33 | adder.add(x); 34 | } 35 | 36 | public void add(List l) { 37 | seen.addAll(l); 38 | adder.add(l); 39 | } 40 | 41 | public int result() { 42 | return adder.result(); 43 | } 44 | 45 | public List log() { 46 | return List.copyOf(seen); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/ced/ec/LogAdderExt.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.ced.ec; 23 | 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | public class LogAdderExt extends Adder { 28 | private final List seen = new ArrayList<>(); 29 | 30 | @Override 31 | public void add(int x) { 32 | seen.add(x); 33 | super.add(x); 34 | } 35 | 36 | @Override 37 | public void add(List l) { 38 | seen.addAll(l); 39 | super.add(l); 40 | } 41 | 42 | public List log() { 43 | return List.copyOf(seen); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/ced/ef/Adder.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.ced.ef; 23 | 24 | import java.util.List; 25 | 26 | public class Adder implements AdderInterface { 27 | 28 | private int result = 0; 29 | 30 | @Override 31 | public void add(int x) { 32 | result += x; 33 | } 34 | 35 | @Override 36 | public void add(List l) { 37 | for (int x : l) add(x); 38 | } 39 | 40 | @Override 41 | public int result() { 42 | return result; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/ced/ef/AdderForwarder.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.ced.ef; 23 | 24 | import java.util.List; 25 | 26 | public class AdderForwarder implements AdderInterface { 27 | 28 | private final AdderInterface adder; 29 | 30 | public AdderForwarder(AdderInterface adder) { 31 | this.adder = adder; 32 | } 33 | 34 | @Override 35 | public void add(int x) { 36 | adder.add(x); 37 | } 38 | 39 | @Override 40 | public void add(List l) { 41 | adder.add(l); 42 | } 43 | 44 | @Override 45 | public int result() { 46 | return adder.result(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/ced/ef/AdderInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.ced.ef; 23 | 24 | import java.util.List; 25 | 26 | public interface AdderInterface { 27 | 28 | public void add(int x); 29 | 30 | public void add(List l); 31 | 32 | public int result(); 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/ced/ef/LogAdder.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.ced.ef; 23 | 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | public class LogAdder extends AdderForwarder { 28 | private final List seen = new ArrayList<>(); 29 | 30 | public LogAdder(AdderInterface adder) { 31 | super(adder); 32 | } 33 | 34 | @Override 35 | public void add(int x) { 36 | seen.add(x); 37 | super.add(x); 38 | } 39 | 40 | @Override 41 | public void add(List l) { 42 | seen.addAll(l); 43 | super.add(l); 44 | } 45 | 46 | public List log() { 47 | return List.copyOf(seen); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/ced/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Per una discussione del codice di questo sottopacchetto si veda Composition & 4 | * Inheritance. 5 | */ 6 | package it.unimi.di.prog2.notes.ced; 7 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/eaco/MainRS.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.eaco; 23 | 24 | import it.unimi.di.prog2.notes.eaco.rs.Histogram; 25 | import it.unimi.di.prog2.notes.eaco.rs.Rectangle; 26 | 27 | public class MainRS { 28 | public static void main(String[] args) { 29 | Histogram hist = new Histogram(); 30 | Rectangle r1 = new Rectangle(4, 4), r2 = new Rectangle(3, 3); 31 | hist.add(r1); 32 | hist.add(r2); 33 | System.out.println(hist); 34 | hist.changeBase(r2, 6); 35 | System.out.println(hist); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/eaco/MainSR.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.eaco; 23 | 24 | import it.unimi.di.prog2.notes.eaco.sr.Histogram; 25 | import it.unimi.di.prog2.notes.eaco.sr.Rectangle; 26 | import it.unimi.di.prog2.notes.eaco.sr.Square; 27 | 28 | public class MainSR { 29 | 30 | public static void main(String[] args) { 31 | 32 | Histogram hist1 = new Histogram(); 33 | Rectangle r1 = new Rectangle(4, 4), r2 = new Rectangle(3, 3); 34 | hist1.add(r1); 35 | hist1.add(r2); 36 | System.out.println(hist1); 37 | hist1.changeBase(r2, 6); 38 | System.out.println(hist1); 39 | 40 | Histogram hist2 = new Histogram(); 41 | Square s1 = new Square(4), s2 = new Square(3); 42 | hist2.add(s1); 43 | hist2.add(s2); 44 | System.out.println(hist2); 45 | hist2.changeBase(s2, 6); 46 | System.out.println(hist2); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/eaco/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Per una discussione del codice di questo sottopacchetto si veda Ontology & Inheritance. 4 | */ 5 | package it.unimi.di.prog2.notes.eaco; 6 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/eaco/rs/Histogram.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.eaco.rs; 23 | 24 | import java.util.LinkedList; 25 | import java.util.List; 26 | import java.util.NoSuchElementException; 27 | 28 | public class Histogram { 29 | 30 | List rectangles = new LinkedList<>(); 31 | 32 | public void add(Rectangle r) { 33 | int i; 34 | for (i = 0; i < rectangles.size(); i++) if (rectangles.get(i).height() > r.height()) break; 35 | rectangles.add(i, r); 36 | } 37 | 38 | public void changeBase(Rectangle o, int base) throws NoSuchElementException { 39 | final int idx = rectangles.indexOf(o); 40 | if (idx != -1) rectangles.get(idx).base(base); 41 | else throw new NoSuchElementException(); 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | return rectangles.toString(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/eaco/rs/Rectangle.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.eaco.rs; 23 | 24 | public class Rectangle extends Square { 25 | private int height; 26 | 27 | public Rectangle(int base, int height) { 28 | super(base); 29 | this.height = height; 30 | } 31 | 32 | public int height() { 33 | return this.height; 34 | } 35 | 36 | public void height(int height) { 37 | this.height = height; 38 | } 39 | 40 | @Override 41 | public String toString() { 42 | return String.format("Rectangle: base = %d, height = %d", base(), height); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/eaco/rs/Square.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.eaco.rs; 23 | 24 | public class Square { 25 | private int base; 26 | 27 | public Square(int base) { 28 | this.base = base; 29 | } 30 | 31 | public int base() { 32 | return this.base; 33 | } 34 | 35 | public void base(int base) { 36 | this.base = base; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return String.format("Square, base = %d", base); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/eaco/sr/Histogram.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.eaco.sr; 23 | 24 | import java.util.LinkedList; 25 | import java.util.List; 26 | import java.util.NoSuchElementException; 27 | 28 | public class Histogram { 29 | 30 | List rectangles = new LinkedList<>(); 31 | 32 | public void add(Rectangle r) { 33 | int i; 34 | for (i = 0; i < rectangles.size(); i++) if (rectangles.get(i).height() > r.height()) break; 35 | rectangles.add(i, r); 36 | } 37 | 38 | public void changeBase(Rectangle o, int base) throws NoSuchElementException { 39 | final int idx = rectangles.indexOf(o); 40 | if (idx != -1) rectangles.get(idx).base(base); 41 | else throw new NoSuchElementException(); 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | return rectangles.toString(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/eaco/sr/Rectangle.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.eaco.sr; 23 | 24 | public class Rectangle { 25 | private int base, height; 26 | 27 | public Rectangle(int base, int height) { 28 | this.base = base; 29 | this.height = height; 30 | } 31 | 32 | public int base() { 33 | return this.base; 34 | } 35 | 36 | public void base(int base) { 37 | this.base = base; 38 | } 39 | 40 | public int height() { 41 | return this.height; 42 | } 43 | 44 | public void height(int height) { 45 | this.height = height; 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return String.format("WrongFigures.Rectangle: base = %d, height = %d", base(), height); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/eaco/sr/Square.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.eaco.sr; 23 | 24 | public class Square extends Rectangle { 25 | 26 | public Square(int base) { 27 | super(base, base); 28 | } 29 | 30 | public void base(int base) { 31 | super.base(base); 32 | super.height(base); 33 | } 34 | 35 | public void height(int height) { 36 | super.base(height); 37 | super.height(height); 38 | } 39 | 40 | @Override 41 | public String toString() { 42 | return String.format("Square, base = %d", base()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/tgerds/Examples.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.tgerds; 23 | 24 | import java.util.List; 25 | 26 | public class Examples { 27 | 28 | List upper; 29 | List lower; 30 | 31 | List n; 32 | List i; 33 | List o; 34 | 35 | List en; 36 | List ei; 37 | List eo; 38 | 39 | List sn; 40 | List si; 41 | List so; 42 | 43 | void upperbound() { 44 | 45 | // prima proprietà (T = Number) 46 | 47 | upper = n; // List is a subtype of List 48 | 49 | // seconda proprietà (T = Number, S = Integer) 50 | 51 | upper = ei; // List is a subtype of List 52 | 53 | // per transitività (T = Number, S = Integer) 54 | 55 | upper = i; // List is a subtype of List 56 | 57 | // per controllo, i due assegnamenti seguenti non compilano 58 | 59 | /* 60 | upper = o; // List isn't a subtype of List 61 | upper = eo; // List isn't a subtype of List 62 | */ 63 | } 64 | 65 | void lowerbound() { 66 | 67 | // prima proprietà (T = Number) 68 | 69 | lower = n; // List is a subtype of List 70 | 71 | // seconda proprietà (T = Object, S = Number) 72 | 73 | lower = so; // List is a subtype of List 74 | 75 | // per transitività (T = Object, S = Number) 76 | 77 | lower = o; // List is a subtype of List 78 | 79 | // per controllo, i due assegnamenti seguenti non compilano 80 | 81 | /* 82 | lower = i; // List isn't a subtype of List 83 | lower = si; // List isn't a subtype of List 84 | */ 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/uee/MainLSP.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.uee; 23 | 24 | import it.unimi.di.prog2.notes.uee.lsp.Client; 25 | import it.unimi.di.prog2.notes.uee.lsp.R; 26 | import it.unimi.di.prog2.notes.uee.lsp.S; 27 | import it.unimi.di.prog2.notes.uee.lsp.T; 28 | 29 | /* 30 | * Per una discussione di questo codice, si veda 31 | * https://prog2.di.unimi.it/notes/equalityandinheritance 32 | */ 33 | 34 | public class MainLSP { 35 | public static void main(String[] args) { 36 | S s = new S(1, 2); 37 | T t = new T(1); 38 | System.out.println(t.equals(s)); 39 | System.out.println(s.equals(t)); 40 | 41 | R r = new R(1); 42 | System.out.println(Client.isSmall(t)); 43 | System.out.println(Client.isSmall(r)); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/uee/MainSym.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.uee; 23 | 24 | import it.unimi.di.prog2.notes.uee.sym.S; 25 | import it.unimi.di.prog2.notes.uee.sym.T; 26 | 27 | /* 28 | * Per una discussione di questo codice, si veda 29 | * https://prog2.di.unimi.it/notes/equalityandinheritance 30 | */ 31 | 32 | public class MainSym { 33 | public static void main(String[] args) { 34 | S s = new S(1, 2); 35 | T t = new T(1); 36 | System.out.println(t.equals(s)); 37 | System.out.println(s.equals(t)); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/uee/MainTrans.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.uee; 23 | 24 | import it.unimi.di.prog2.notes.uee.trans.S; 25 | import it.unimi.di.prog2.notes.uee.trans.T; 26 | 27 | /* 28 | * Per una discussione di questo codice, si veda 29 | * https://prog2.di.unimi.it/notes/equalityandinheritance 30 | */ 31 | 32 | public class MainTrans { 33 | public static void main(String[] args) { 34 | S s = new S(1, 2); 35 | T t = new T(1); 36 | S u = new S(1, 3); 37 | System.out.println(s.equals(t)); 38 | System.out.println(t.equals(u)); 39 | System.out.println(s.equals(u)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/uee/lsp/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.uee.lsp; 23 | 24 | /* 25 | * Per una discussione di questo codice, si veda 26 | * https://prog2.di.unimi.it/notes/equalityandinheritance 27 | */ 28 | 29 | import java.util.Set; 30 | 31 | public class Client { 32 | private static final Set SMALLS = Set.of(new T(1), new T(2)); 33 | 34 | public static boolean isSmall(T t) { 35 | return SMALLS.contains(t); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/uee/lsp/R.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.uee.lsp; 23 | 24 | /* 25 | * Per una discussione di questo codice, si veda 26 | * https://prog2.di.unimi.it/notes/equalityandinheritance 27 | */ 28 | 29 | public class R extends T { 30 | public R(int a) { 31 | super(a); 32 | } 33 | 34 | public void sayHi() { 35 | System.out.println("Hi!"); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/uee/lsp/S.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.uee.lsp; 23 | 24 | /* 25 | * Per una discussione di questo codice, si veda 26 | * https://prog2.di.unimi.it/notes/equalityandinheritance 27 | */ 28 | 29 | public class S extends T { 30 | private final int b; 31 | 32 | public S(int a, int b) { 33 | super(a); 34 | this.b = b; 35 | } 36 | 37 | public boolean equals(Object obj) { 38 | if (obj == null) return false; 39 | if (getClass() == obj.getClass()) { 40 | final S s = (S) obj; 41 | return super.equals(obj) && b == s.b; 42 | } 43 | return false; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/uee/lsp/T.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.uee.lsp; 23 | 24 | /* 25 | * Per una discussione di questo codice, si veda 26 | * https://prog2.di.unimi.it/notes/equalityandinheritance 27 | */ 28 | 29 | public class T { 30 | private final int a; 31 | 32 | public T(int a) { 33 | this.a = a; 34 | } 35 | 36 | public boolean equals(Object obj) { 37 | if (obj == null) return false; 38 | if (getClass() == obj.getClass()) { 39 | final T t = (T) obj; 40 | return a == t.a; 41 | } 42 | return false; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/uee/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Per una discussione del codice di questo sottopacchetto si veda Equality & Inheritance. 4 | */ 5 | package it.unimi.di.prog2.notes.uee; 6 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/uee/sym/S.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.uee.sym; 23 | 24 | /* 25 | * Per una discussione di questo codice, si veda 26 | * https://prog2.di.unimi.it/notes/equalityandinheritance 27 | */ 28 | 29 | public class S extends T { 30 | private final int b; 31 | 32 | public S(int a, int b) { 33 | super(a); 34 | this.b = b; 35 | } 36 | 37 | public boolean equals(Object obj) { 38 | if (obj instanceof S other) return super.equals(obj) && b == other.b; 39 | return false; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/uee/sym/T.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.uee.sym; 23 | 24 | /* 25 | * Per una discussione di questo codice, si veda 26 | * https://prog2.di.unimi.it/notes/equalityandinheritance 27 | */ 28 | 29 | public class T { 30 | private final int a; 31 | 32 | public T(int a) { 33 | this.a = a; 34 | } 35 | 36 | public boolean equals(Object obj) { 37 | if (obj instanceof T other) return a == other.a; 38 | return false; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/uee/trans/S.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.uee.trans; 23 | 24 | /* 25 | * Per una discussione di questo codice, si veda 26 | * https://prog2.di.unimi.it/notes/equalityandinheritance 27 | */ 28 | 29 | public class S extends T { 30 | private final int b; 31 | 32 | public S(int a, int b) { 33 | super(a); 34 | this.b = b; 35 | } 36 | 37 | public boolean equals(Object obj) { 38 | if (obj instanceof S other) return super.equals(obj) && b == other.b; 39 | if (obj instanceof T) return super.equals(obj); 40 | return false; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/it/unimi/di/prog2/notes/uee/trans/T.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright 2024 Massimo Santini 4 | 5 | This file is part of "Programmazione 2 @ UniMI" teaching material. 6 | 7 | This is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This material is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this file. If not, see . 19 | 20 | */ 21 | 22 | package it.unimi.di.prog2.notes.uee.trans; 23 | 24 | /* 25 | * Per una discussione di questo codice, si veda 26 | * https://prog2.di.unimi.it/notes/equalityandinheritance 27 | */ 28 | 29 | public class T { 30 | private final int a; 31 | 32 | public T(int a) { 33 | this.a = a; 34 | } 35 | 36 | public boolean equals(Object obj) { 37 | if (obj instanceof T other) return a == other.a; 38 | return false; 39 | } 40 | } 41 | --------------------------------------------------------------------------------