Skip to content
Snippets Groups Projects
Commit d0d180b6 authored by Bruno Zanuttini's avatar Bruno Zanuttini
Browse files

Initial commit: source code for first publicly released version (PepTraq 2.0, March 2022)

parent 159347f0
Branches
Tags peptraq2.0
No related merge requests found
Showing
with 2182 additions and 79 deletions
CeCILL FREE SOFTWARE LICENSE AGREEMENT
Version 2.1 dated 2013-06-21
Notice
This Agreement is a Free Software license agreement that is the result of discussions between its authors in order to ensure compliance with the two main principles guiding its drafting:
* firstly, compliance with the principles governing the distribution of Free Software: access to source code, broad rights granted to users,
* secondly, the election of a governing law, French law, with which it is conformant, both as regards the law of torts and intellectual property law, and the protection that it offers to both authors and holders of the economic rights over software.
The authors of the CeCILL (for Ce[a] C[nrs] I[nria] L[ogiciel] L[ibre]) license are:
Commissariat à l'énergie atomique et aux énergies alternatives - CEA, a public scientific, technical and industrial research establishment, having its principal place of business at 25 rue Leblanc, immeuble Le Ponant D, 75015 Paris, France.
Centre National de la Recherche Scientifique - CNRS, a public scientific and technological establishment, having its principal place of business at 3 rue Michel-Ange, 75794 Paris cedex 16, France.
Institut National de Recherche en Informatique et en Automatique - Inria, a public scientific and technological establishment, having its principal place of business at Domaine de Voluceau, Rocquencourt, BP 105, 78153 Le Chesnay cedex, France.
Preamble
The purpose of this Free Software license agreement is to grant users the right to modify and redistribute the software governed by this license within the framework of an open source distribution model.
The exercising of this right is conditional upon certain obligations for users so as to preserve this status for all subsequent redistributions.
In consideration of access to the source code and the rights to copy, modify and redistribute granted by the license, users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors only have limited liability.
In this respect, the risks associated with loading, using, modifying and/or developing or reproducing the software by the user are brought to the user's attention, given its Free Software status, which may make it complicated to use, with the result that its use is reserved for developers and experienced professionals having in-depth computer knowledge. Users are therefore encouraged to load and test the suitability of the software as regards their requirements in conditions enabling the security of their systems and/or data to be ensured and, more generally, to use and operate it in the same conditions of security. This Agreement may be freely reproduced and published, provided it is not altered, and that no provisions are either added or removed herefrom.
This Agreement may apply to any or all software for which the holder of the economic rights decides to submit the use thereof to its provisions.
Frequently asked questions can be found on the official website of the CeCILL licenses family (http://www.cecill.info/index.en.html) for any necessary clarification.
Article 1 - DEFINITIONS
For the purpose of this Agreement, when the following expressions commence with a capital letter, they shall have the following meaning:
Agreement: means this license agreement, and its possible subsequent versions and annexes.
Software: means the software in its Object Code and/or Source Code form and, where applicable, its documentation, "as is" when the Licensee accepts the Agreement.
Initial Software: means the Software in its Source Code and possibly its Object Code form and, where applicable, its documentation, "as is" when it is first distributed under the terms and conditions of the Agreement.
Modified Software: means the Software modified by at least one Contribution.
Source Code: means all the Software's instructions and program lines to which access is required so as to modify the Software.
Object Code: means the binary files originating from the compilation of the Source Code.
Holder: means the holder(s) of the economic rights over the Initial Software.
Licensee: means the Software user(s) having accepted the Agreement.
Contributor: means a Licensee having made at least one Contribution.
Licensor: means the Holder, or any other individual or legal entity, who distributes the Software under the Agreement.
Contribution: means any or all modifications, corrections, translations, adaptations and/or new functions integrated into the Software by any or all Contributors, as well as any or all Internal Modules.
Module: means a set of sources files including their documentation that enables supplementary functions or services in addition to those offered by the Software.
External Module: means any or all Modules, not derived from the Software, so that this Module and the Software run in separate address spaces, with one calling the other when they are run.
Internal Module: means any or all Module, connected to the Software so that they both execute in the same address space.
GNU GPL: means the GNU General Public License version 2 or any subsequent version, as published by the Free Software Foundation Inc.
GNU Affero GPL: means the GNU Affero General Public License version 3 or any subsequent version, as published by the Free Software Foundation Inc.
EUPL: means the European Union Public License version 1.1 or any subsequent version, as published by the European Commission.
Parties: mean both the Licensee and the Licensor.
These expressions may be used both in singular and plural form.
Article 2 - PURPOSE
The purpose of the Agreement is the grant by the Licensor to the Licensee of a non-exclusive, transferable and worldwide license for the Software as set forth in Article 5 <#scope> hereinafter for the whole term of the protection granted by the rights over said Software.
Article 3 - ACCEPTANCE
3.1 The Licensee shall be deemed as having accepted the terms and conditions of this Agreement upon the occurrence of the first of the following events:
* (i) loading the Software by any or all means, notably, by downloading from a remote server, or by loading from a physical medium;
* (ii) the first time the Licensee exercises any of the rights granted hereunder.
3.2 One copy of the Agreement, containing a notice relating to the characteristics of the Software, to the limited warranty, and to the fact that its use is restricted to experienced users has been provided to the Licensee prior to its acceptance as set forth in Article 3.1 <#accepting> hereinabove, and the Licensee hereby acknowledges that it has read and understood it.
Article 4 - EFFECTIVE DATE AND TERM
4.1 EFFECTIVE DATE
The Agreement shall become effective on the date when it is accepted by the Licensee as set forth in Article 3.1 <#accepting>.
4.2 TERM
The Agreement shall remain in force for the entire legal term of protection of the economic rights over the Software.
Article 5 - SCOPE OF RIGHTS GRANTED
The Licensor hereby grants to the Licensee, who accepts, the following rights over the Software for any or all use, and for the term of the Agreement, on the basis of the terms and conditions set forth hereinafter.
Besides, if the Licensor owns or comes to own one or more patents protecting all or part of the functions of the Software or of its components, the Licensor undertakes not to enforce the rights granted by these patents against successive Licensees using, exploiting or modifying the Software. If these patents are transferred, the Licensor undertakes to have the transferees subscribe to the obligations set forth in this paragraph.
5.1 RIGHT OF USE
The Licensee is authorized to use the Software, without any limitation as to its fields of application, with it being hereinafter specified that this comprises:
1. permanent or temporary reproduction of all or part of the Software by any or all means and in any or all form.
2. loading, displaying, running, or storing the Software on any or all medium.
3. entitlement to observe, study or test its operation so as to determine the ideas and principles behind any or all constituent elements of said Software. This shall apply when the Licensee carries out any or all loading, displaying, running, transmission or storage operation as regards the Software, that it is entitled to carry out hereunder.
5.2 ENTITLEMENT TO MAKE CONTRIBUTIONS
The right to make Contributions includes the right to translate, adapt, arrange, or make any or all modifications to the Software, and the right to reproduce the resulting software.
The Licensee is authorized to make any or all Contributions to the Software provided that it includes an explicit notice that it is the author of said Contribution and indicates the date of the creation thereof.
5.3 RIGHT OF DISTRIBUTION
In particular, the right of distribution includes the right to publish, transmit and communicate the Software to the general public on any or all medium, and by any or all means, and the right to market, either in consideration of a fee, or free of charge, one or more copies of the Software by any means.
The Licensee is further authorized to distribute copies of the modified or unmodified Software to third parties according to the terms and conditions set forth hereinafter.
5.3.1 DISTRIBUTION OF SOFTWARE WITHOUT MODIFICATION
The Licensee is authorized to distribute true copies of the Software in Source Code or Object Code form, provided that said distribution complies with all the provisions of the Agreement and is accompanied by:
1. a copy of the Agreement,
2. a notice relating to the limitation of both the Licensor's warranty and liability as set forth in Articles 8 and 9,
and that, in the event that only the Object Code of the Software is redistributed, the Licensee allows effective access to the full Source Code of the Software for a period of at least three years from the distribution of the Software, it being understood that the additional acquisition cost of the Source Code shall not exceed the cost of the data transfer.
5.3.2 DISTRIBUTION OF MODIFIED SOFTWARE
When the Licensee makes a Contribution to the Software, the terms and conditions for the distribution of the resulting Modified Software become subject to all the provisions of this Agreement.
The Licensee is authorized to distribute the Modified Software, in source code or object code form, provided that said distribution complies with all the provisions of the Agreement and is accompanied by:
1. a copy of the Agreement,
2. a notice relating to the limitation of both the Licensor's warranty and liability as set forth in Articles 8 and 9,
and, in the event that only the object code of the Modified Software is redistributed,
3. a note stating the conditions of effective access to the full source code of the Modified Software for a period of at least three years from the distribution of the Modified Software, it being understood that the additional acquisition cost of the source code shall not exceed the cost of the data transfer.
5.3.3 DISTRIBUTION OF EXTERNAL MODULES
When the Licensee has developed an External Module, the terms and conditions of this Agreement do not apply to said External Module, that may be distributed under a separate license agreement.
5.3.4 COMPATIBILITY WITH OTHER LICENSES
The Licensee can include a code that is subject to the provisions of one of the versions of the GNU GPL, GNU Affero GPL and/or EUPL in the Modified or unmodified Software, and distribute that entire code under the terms of the same version of the GNU GPL, GNU Affero GPL and/or EUPL.
The Licensee can include the Modified or unmodified Software in a code that is subject to the provisions of one of the versions of the GNU GPL, GNU Affero GPL and/or EUPL and distribute that entire code under the terms of the same version of the GNU GPL, GNU Affero GPL and/or EUPL.
Article 6 - INTELLECTUAL PROPERTY
6.1 OVER THE INITIAL SOFTWARE
The Holder owns the economic rights over the Initial Software. Any or all use of the Initial Software is subject to compliance with the terms and conditions under which the Holder has elected to distribute its work and no one shall be entitled to modify the terms and conditions for the distribution of said Initial Software.
The Holder undertakes that the Initial Software will remain ruled at least by this Agreement, for the duration set forth in Article 4.2 <#term>.
6.2 OVER THE CONTRIBUTIONS
The Licensee who develops a Contribution is the owner of the intellectual property rights over this Contribution as defined by applicable law.
6.3 OVER THE EXTERNAL MODULES
The Licensee who develops an External Module is the owner of the intellectual property rights over this External Module as defined by applicable law and is free to choose the type of agreement that shall govern its distribution.
6.4 JOINT PROVISIONS
The Licensee expressly undertakes:
1. not to remove, or modify, in any manner, the intellectual property notices attached to the Software;
2. to reproduce said notices, in an identical manner, in the copies of the Software modified or not.
The Licensee undertakes not to directly or indirectly infringe the intellectual property rights on the Software of the Holder and/or Contributors, and to take, where applicable, vis-à-vis its staff, any and all measures required to ensure respect of said intellectual property rights of the Holder and/or Contributors.
Article 7 - RELATED SERVICES
7.1 Under no circumstances shall the Agreement oblige the Licensor to provide technical assistance or maintenance services for the Software.
However, the Licensor is entitled to offer this type of services. The terms and conditions of such technical assistance, and/or such maintenance, shall be set forth in a separate instrument. Only the Licensor offering said maintenance and/or technical assistance services shall incur liability therefor.
7.2 Similarly, any Licensor is entitled to offer to its licensees, under its sole responsibility, a warranty, that shall only be binding upon itself, for the redistribution of the Software and/or the Modified Software, under terms and conditions that it is free to decide. Said warranty, and the financial terms and conditions of its application, shall be subject of a separate instrument executed between the Licensor and the Licensee.
Article 8 - LIABILITY
8.1 Subject to the provisions of Article 8.2, the Licensee shall be entitled to claim compensation for any direct loss it may have suffered from the Software as a result of a fault on the part of the relevant Licensor, subject to providing evidence thereof.
8.2 The Licensor's liability is limited to the commitments made under this Agreement and shall not be incurred as a result of in particular: (i) loss due the Licensee's total or partial failure to fulfill its obligations, (ii) direct or consequential loss that is suffered by the Licensee due to the use or performance of the Software, and (iii) more generally, any consequential loss. In particular the Parties expressly agree that any or all pecuniary or business loss (i.e. loss of data, loss of profits, operating loss, loss of customers or orders, opportunity cost, any disturbance to business activities) or any or all legal proceedings instituted against the Licensee by a third party, shall constitute consequential loss and shall not provide entitlement to any or all compensation from the Licensor.
Article 9 - WARRANTY
9.1 The Licensee acknowledges that the scientific and technical state-of-the-art when the Software was distributed did not enable all possible uses to be tested and verified, nor for the presence of possible defects to be detected. In this respect, the Licensee's attention has been drawn to the risks associated with loading, using, modifying and/or developing and reproducing the Software which are reserved for experienced users.
The Licensee shall be responsible for verifying, by any or all means, the suitability of the product for its requirements, its good working order, and for ensuring that it shall not cause damage to either persons or properties.
9.2 The Licensor hereby represents, in good faith, that it is entitled to grant all the rights over the Software (including in particular the rights set forth in Article 5 <#scope>).
9.3 The Licensee acknowledges that the Software is supplied "as is" by the Licensor without any other express or tacit warranty, other than that provided for in Article 9.2 <#good-faith> and, in particular, without any warranty as to its commercial value, its secured, safe, innovative or relevant nature.
Specifically, the Licensor does not warrant that the Software is free from any error, that it will operate without interruption, that it will be compatible with the Licensee's own equipment and software configuration, nor that it will meet the Licensee's requirements.
9.4 The Licensor does not either expressly or tacitly warrant that the Software does not infringe any third party intellectual property right relating to a patent, software or any other property right. Therefore, the Licensor disclaims any and all liability towards the Licensee arising out of any or all proceedings for infringement that may be instituted in respect of the use, modification and redistribution of the Software. Nevertheless, should such proceedings be instituted against the Licensee, the Licensor shall provide it with technical and legal expertise for its defense. Such technical and legal expertise shall be decided on a case-by-case basis between the relevant Licensor and the Licensee pursuant to a memorandum of understanding. The Licensor disclaims any and all liability as regards the Licensee's use of the name of the Software. No warranty is given as regards the existence of prior rights over the name of the Software or as regards the existence of a trademark.
Article 10 - TERMINATION
10.1 In the event of a breach by the Licensee of its obligations hereunder, the Licensor may automatically terminate this Agreement thirty (30) days after notice has been sent to the Licensee and has remained ineffective.
10.2 A Licensee whose Agreement is terminated shall no longer be authorized to use, modify or distribute the Software. However, any licenses that it may have granted prior to termination of the Agreement shall remain valid subject to their having been granted in compliance with the terms and conditions hereof.
Article 11 - MISCELLANEOUS
11.1 EXCUSABLE EVENTS
Neither Party shall be liable for any or all delay, or failure to perform the Agreement, that may be attributable to an event of force majeure, an act of God or an outside cause, such as defective functioning or interruptions of the electricity or telecommunications networks, network paralysis following a virus attack, intervention by government authorities, natural disasters, water damage, earthquakes, fire, explosions, strikes and labor unrest, war, etc.
11.2 Any failure by either Party, on one or more occasions, to invoke one or more of the provisions hereof, shall under no circumstances be interpreted as being a waiver by the interested Party of its right to invoke said provision(s) subsequently.
11.3 The Agreement cancels and replaces any or all previous agreements, whether written or oral, between the Parties and having the same purpose, and constitutes the entirety of the agreement between said Parties concerning said purpose. No supplement or modification to the terms and conditions hereof shall be effective as between the Parties unless it is made in writing and signed by their duly authorized representatives.
11.4 In the event that one or more of the provisions hereof were to conflict with a current or future applicable act or legislative text, said act or legislative text shall prevail, and the Parties shall make the necessary amendments so as to comply with said act or legislative text. All other provisions shall remain effective. Similarly, invalidity of a provision of the Agreement, for any reason whatsoever, shall not cause the Agreement as a whole to be invalid.
11.5 LANGUAGE
The Agreement is drafted in both French and English and both versions are deemed authentic.
Article 12 - NEW VERSIONS OF THE AGREEMENT
12.1 Any person is authorized to duplicate and distribute copies of this Agreement.
12.2 So as to ensure coherence, the wording of this Agreement is protected and may only be modified by the authors of the License, who reserve the right to periodically publish updates or new versions of the Agreement, each with a separate number. These subsequent versions may address new issues encountered by Free Software.
12.3 Any Software distributed under a given version of the Agreement may only be subsequently distributed under the same version of the Agreement or a subsequent version, subject to the provisions of Article 5.3.4 <#compatibility>.
Article 13 - GOVERNING LAW AND JURISDICTION
13.1 The Agreement is governed by French law. The Parties agree to endeavor to seek an amicable solution to any disagreements or disputes that may arise during the performance of the Agreement.
13.2 Failing an amicable solution within two (2) months as from their occurrence, and unless emergency proceedings are necessary, the disagreements or disputes shall be referred to the Paris Courts having jurisdiction, by the more diligent Party.
......@@ -2,91 +2,29 @@
PepTraq is a desktop application enabling biologists to explore genomes, transcriptomes, and protein banks easily, by applying filters and transformations, saving searches at any time in fasta format, etc. This repository hosts the source code.
## Getting started
# Authors
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
PepTraq is developed at the Université de Caen Normandie, France, in laboratories GREYC (Normandie Université; UNICAEN, CNRS UMR 6072, ENSICAEN) and BOREA (MNHN, CNRS 8067, SU,
IRD 207, UCN, UA). It was also supported by a grant by Région Normandie (Projet Émergent n°16E00790/16P03508, 2017-2019).
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
The authors are:
## Add your files
* Bruno Zanuttini (GREYC, Université de Caen Normandie),
* Céline Zatylny-Gaudin (BOREA, Université de Caen Normandie),
* Joël Henry (BOREA, Université de Caen Normandie),
* Christophe Couronne (GREYC, CNRS),
* Abdelkader Ouali (GREYC, Université de Caen Normandie).
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
LICENSE
```
cd existing_repo
git remote add origin https://git.unicaen.fr/bruno.zanuttini/peptraq.git
git branch -M main
git push -uf origin main
```
PepTraq is distributed open source under the CeCILL-B license. It embeds other libraries (see lib/README.txt).
## Integrate with your tools
REPOSITORY
- [ ] [Set up project integrations](https://git.unicaen.fr/bruno.zanuttini/peptraq/-/settings/integrations)
This repository is organized as follows:
## Collaborate with your team
* aasuite is standalone, and provides classes which are useful independently of the desktop application (in particular, they are used in the web version of PepTraq)
* peptraq is the desktop application, which uses aasuite
* scripts gives scripts for compiling, packaging, and documenting either aasuite standlone, or the whole of PepTraq desktop
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
Prior to compiling, the dependencies specified in lib/README.txt must be added to the lib/ folder.
package fr.greyc.mad.datastructures;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
/**
* A sequence of elements which additionally maintains a current index. The
* index allows one to focus on a specific position of the sequence, mostly as
* if it were (temporarily) the end of the sequence.
* <p>
* This structure is designed for applications such as navigation between pages
* in a web browser. As long as the sequence does not change, the current index
* can be moved backwards and forward. Addition and removal of elements are
* defined as if the current index denoted the end of the sequence, so that
* addition occurs right after the current index, and removal occurs at the
* current index. The current index is moved forward or backwards, respectively,
* and in both cases the tail of the sequence is removed (think of changing
* current page invalidating the "forward" button in a web browser).
* <p>
* Indices start from 0, and may also be null (no position focused at, to be
* seen for all purposes as the current index being just before the beginning of
* the sequence).
* <p>
* Note that this class does NOT implement {@code java.util.List}, since
* modifications are heavily dependent on the current index. Another way to see
* instances of this class is as stacks, with the property that as long as the
* sequence itself does not change, the top of the stack can be defined to be at
* any position in the sequence.
*
* @author Bruno Zanuttini, Université de Caen Normandie, France
*
* @param <E> The type of elements in the sequence
*
*/
public class InteractiveSequence<E> {
/** A list to which storage of elements is delegated. */
private final List<E> list;
/** The current index (null if none). */
private Integer currentIndex;
/**
* Builds a new instance with no element.
*/
public InteractiveSequence() {
this(new ArrayList<>());
}
/**
* Builds a new instance from a given list and no current index. The given list
* is backed up by this sequence, so that it should not be modified anywhere
* else (the behavior of this sequence would be undefined after this).
*
* @param list A list
*/
public InteractiveSequence(List<E> list) {
this(list, null);
}
/**
* Builds a new instance from a given list and a given current index. The given
* list is backed up by this one, so that it should not be modified anywhere
* else (the behavior of this sequence would be undefined after this).
* <p>
* The list is suppose to allow additional operations of addition, removal,
* etc., otherwise such operations will not be supported either by this
* sequence.
*
* @param list A list
* @param index An index in the list
* @throws IllegalArgumentException if the index is not a valid one in the list
*/
public InteractiveSequence(List<E> list, int index) throws IllegalArgumentException {
this(list, Integer.valueOf(index));
}
/**
* Returns the size of this sequence, that is, its number of elements,
* independently of the current index.
* <p>
* The list is suppose to allow additional operations of addition, removal,
* etc., otherwise such operations will not be supported either by this
* sequence.
*
* @return The size of this sequence
*/
public int size() {
return this.list.size();
}
/**
* Decides whether this sequence is empty.
*
* @return true if this sequence is empty, false otherwise
*/
public boolean isEmpty() {
return this.list.isEmpty();
}
/**
* Returns the element at a given index in this sequence.
*
* @param i An index in this sequence
* @return The element at the given index in this sequence
* @throws IndexOutOfBoundsException if the index is not valid for this sequence
*/
public E get(int i) throws IndexOutOfBoundsException {
return this.list.get(i);
}
/**
* Decides whether there is a current index.
*
* @return true if there is a current index, false otherwise
*/
public boolean hasCurrentIndex() {
return this.currentIndex != null;
}
/**
* Returns the current index.
*
* @return The current index
* @throws IllegalStateException if there is no current index
*/
public int getCurrentIndex() throws IllegalStateException {
if (this.currentIndex == null) {
throw new IllegalStateException("There is no current index");
}
return this.currentIndex;
}
/**
* Returns the element at the current index.
*
* @return The element at the current index
* @throws IllegalStateException if there is no current index
*/
public E getCurrent() throws IllegalStateException {
if (this.currentIndex == null) {
throw new IllegalStateException("There is no current index");
}
return this.list.get(this.currentIndex);
}
/**
* Moves the current index to a given position.
*
* @param index The target position
* @throws IndexOutOfBoundsException if the target index is not valid
*/
public void to(int index) throws IndexOutOfBoundsException {
if (index < 0 || this.list.size() <= index) {
throw new IndexOutOfBoundsException("Invalid index for sequence size: " + index + ", " + this.list.size());
}
this.currentIndex = index;
}
/**
* Adds an element right after the current index, and moves the index forward by
* 1, so that it corresponds to the new element. If there is no current index,
* then the element is inserted as the first element of the sequence, and the
* index becomes 0. All elements after the one added are discarded from the
* sequence.
*
* @param element An element
*/
public void extendFromCurrent(E element) {
this.removeTail();
this.list.add(element);
if (this.currentIndex == null) {
this.currentIndex = 0;
} else {
this.currentIndex++;
}
}
/**
* Removes the element at the current index, and moves the index backwards by 1.
* If the current index was 0, there is no current index after this call. All
* elements after the one added are discarded from the sequence.
*
* @throws IllegalStateException if there is no current index
*/
public void removeCurrent() throws IllegalStateException {
if (this.currentIndex == null) {
throw new IllegalStateException("No current element to remove");
}
this.removeTail();
// Next instruction: intValue enforces call to remove(int) instead of
// remove(Object)
this.list.remove(this.currentIndex.intValue());
this.currentIndex--;
}
/**
* Clears this sequence (and as a consequence, unsets the current index).
*/
public void clear() {
this.list.clear();
this.currentIndex = null;
this.removeTail();
}
/**
* Builds a new instance from the elements in a list.
*
* @param list A list of elements
* @param indexOrNull The current index to define, or null for none
* @throws IndexOutOfBoundsException If the index is not valid for the list
*/
private InteractiveSequence(List<E> list, Integer indexOrNull) throws IndexOutOfBoundsException {
if (indexOrNull != null && (indexOrNull < 0 || list.size() <= indexOrNull)) {
throw new IndexOutOfBoundsException("Invalid index xwrt list size: " + indexOrNull + ", " + list.size());
}
this.list = list;
this.currentIndex = indexOrNull;
}
/**
* Helper methods: removes all elements after the current index (all elements if
* there is no current index).
*/
private void removeTail() {
final int lastPreservedIndex = this.currentIndex == null ? -1 : this.currentIndex;
ListIterator<E> iterator = this.list.listIterator(this.list.size());
// For all rounds, i is the position at which an element is removed at this
// round
for (int i = this.list.size() - 1; i > lastPreservedIndex; i--) {
iterator.previous();
iterator.remove();
}
}
}
package fr.greyc.mad.datastructures;
/**
* A class for representing a finite, nonempty interval of integers. Such
* intervals are comparable: [x, y[ is less than [z, t[ if and only if x &lt; z
* holds, or x = z and y &lt; t hold.
*
* @author Bruno Zanuttini, Université de Caen Normandie, France
*
* TODO: equals, hashCode
*/
public class Interval implements Comparable<Interval> {
/** The lowest element in the interval. */
protected final int begin;
/** The greatest element in the interval + 1. */
protected final int end;
/**
* Builds a new instance [begin, end[.
*
* @param begin The lowest element in the interval
* @param end The greatest element in the interval + 1
* @throws IllegalArgumentException if end is less than or equal to begin (which
* would result in a nonempty interval).
*/
public Interval(int begin, int end) throws IllegalArgumentException {
if (end <= begin) {
throw new IllegalArgumentException("Cannot build an empty interval: " + begin + ", " + end);
}
this.begin = begin;
this.end = end;
}
/**
* Builds a new interval containing a single integer.
*
* @param i an integer
* @throws IllegalArgumentException if the given integer is Integer.MAX_VALUE
*/
public Interval(int i) throws IllegalArgumentException {
this(i, i + 1);
if (i == Integer.MAX_VALUE) {
throw new IllegalArgumentException("Cannot build an interval containing only Integer.MAX_VALUE");
}
}
/**
* Returns the lowest element in this interval.
*
* @return The lowest element in this interval
*/
public int getBegin() {
return this.begin;
}
/**
* Returns the greatest element in this interval + 1.
*
* @return The greatest element in this interval + 1
*/
public int getEnd() {
return this.end;
}
/**
* Decides whether a given integer is in this interval.
*
* @param i An integer
* @return true if this interval contains i, false otherwise
*/
public boolean contains(int i) {
return this.begin <= i && i < this.end;
}
@Override
public int compareTo(Interval other) {
return this.begin != other.begin ? Integer.compare(this.begin, other.begin)
: Integer.compare(this.end, other.end);
}
@Override
public String toString() {
return "[" + this.begin + ", " + this.end + "]";
}
}
package fr.greyc.mad.datastructures;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
/**
* A base class for a lazy representation of a iterable collection of elements.
* Such a representation behaves like a list, except that an element is
* explicited only the first time when it is accessed; explicitation is defined
* by concrete subclasses.
* <p>
* This representation can be seen as a Java stream, but importantly, it is not
* consumed when the elements are accessed, so that they can be accessed over
* and over without expliciting them again.
* <p>
* This representation is supposed to be very efficient when the elements of the
* list are the result of heavy computations (explicitation). Note however that
* there might be counterintuitive behaviors, like an empty list being very long
* to iterate over because a huge number of computations need be done which
* produce no element in the end.
*
* @author Bruno Zanuttini, Université de Caen Normandie, France
*
* @param <E> The type of elements
*/
public abstract class LazyIterable<E> implements Iterable<E> {
/** The list of elements already explicited (a prefix of the collection). */
protected ArrayList<E> explicited;
/**
* Builds a new instance with no element.
*/
public LazyIterable() {
this.explicited = new ArrayList<>();
}
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
private int i = 0;
private boolean explicitUpTo(int i) {
while (LazyIterable.this.explicited.size() <= i) {
List<E> newExplicit = LazyIterable.this.tryExplicitMore();
if (newExplicit.isEmpty()) {
return false;
} else {
LazyIterable.this.explicited.addAll(newExplicit);
}
}
return true;
}
@Override
public boolean hasNext() {
return this.explicitUpTo(i);
}
@Override
public E next() {
boolean ok = this.explicitUpTo(i);
if (ok) {
return LazyIterable.this.explicited.get(i++);
} else {
throw new NoSuchElementException();
}
}
};
}
/**
* Explicits a number of elements after the ones already explicit. How many
* elements to explicit is left to concrete implementations, however, at least 1
* element should be explicited except if there are no more elements to
* explicit.
*
* @return A list containing at least one new explicit element if there is one,
* and en empty list otherwise
*/
protected abstract List<E> tryExplicitMore();
}
package fr.greyc.mad.datastructures;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
/**
* A class for representing "pages" of elements, that is, sublists starting and
* ending at given indices, as in paginated user interfaces, though without any
* notion of representation for the user.
* <p>
* A page is associated with a page number, and first and last element indices
* (relative to the whole list of which this is a page). In general, it is
* conceived as part of a whole, and accordingly, it also knows whether it has a
* previous and/or a next page. The last page of a collection may have fewer
* elements than the given size of a page.
* <p>
* A page can never be empty. In particular, an empty list of elements has no
* page at all, whatever the page size, and the page size cannot be 0.
*
* @author Bruno Zanuttini, Université de Caen Normandie, France
*
* @param <E> The type of elements in the page
*/
public class Page<E> {
/** The list of elements in this page. */
private List<E> pageElements;
/** The number of the previous page (null if none). */
private Integer previousPageNumber;
/** The number of the next page (null if none). */
private Integer nextPageNumber;
/**
* The index of the first element of this page in the whole list (0-indexed,
* inclusive).
*/
private int firstElementIndex;
/**
* The index of the last element of this page in the whole list (0-indexed,
* exclusive).
*/
private int lastElementIndex;
/**
* Returns the number of pages for representing a collection. In the specific
* case when there are no elements, the number of pages is 0.
*
* @param elements The whole collection of elements
* @param pageSize The size of each page
* @return The number of pages for representing a collection with the given page
* size
* @throws IllegalArgumentException if the page size is not (strictly) positive
*/
public static int getNbPages(Collection<?> elements, int pageSize) throws IllegalArgumentException {
if (pageSize <= 0) {
throw new IllegalArgumentException();
}
int res = elements.size() / pageSize;
return elements.size() % pageSize == 0 ? res : res + 1;
}
/**
* Builds a new instance. For efficiency, constructor
* {@link #Page(List, int, int)} should be preferred since it only creates a
* view of the list instead of retrieving and copying the elements. This
* constructor should be used only when the elements can be itered over but are
* not already stored as a list.
*
* @param elements The whole list of elements
* @param pageNumber The number of the page to build (0-indexed)
* @param pageSize The size of each page
* @throws IndexOutOfBoundsException if the page number is not valid (negative,
* or geq the number of pages), hence in
* particular if there are no elements at all
*/
public Page(Iterable<E> elements, int pageNumber, int pageSize) throws IndexOutOfBoundsException {
if (pageNumber < 0) {
throw new IndexOutOfBoundsException("" + pageNumber);
}
this.firstElementIndex = pageSize * pageNumber;
this.previousPageNumber = pageNumber == 0 ? null : pageNumber - 1;
int i = 0;
Iterator<E> iterator = elements.iterator();
while (iterator.hasNext() && i < pageNumber * pageSize) {
iterator.next();
i++;
}
if (!iterator.hasNext()) {
throw new IndexOutOfBoundsException("" + pageNumber);
} else {
int upperBound = pageSize * (pageNumber + 1);
this.pageElements = new ArrayList<>();
while (iterator.hasNext() && i < upperBound) {
this.pageElements.add(iterator.next());
i++;
}
this.lastElementIndex = i;
this.nextPageNumber = iterator.hasNext() ? pageNumber + 1 : null;
}
}
/**
* Builds a new instance.
*
* @param list The whole list of elements
* @param pageNumber The number of the page to build (0-indexed)
* @param pageSize The size of each page
* @throws IndexOutOfBoundsException if the page number is not valid (negative,
* or geq the number of pages), hence in
* particular if the list of elements is empty
*/
public Page(List<E> list, int pageNumber, int pageSize) throws IndexOutOfBoundsException {
this.firstElementIndex = pageSize * pageNumber;
if (this.firstElementIndex >= list.size()) {
throw new IndexOutOfBoundsException(
"page number " + pageNumber + " for list of size " + list.size() + " with pageSize " + pageSize);
}
this.lastElementIndex = Math.min(pageSize * (pageNumber + 1), list.size());
this.previousPageNumber = pageNumber == 0 ? null : pageNumber - 1;
this.nextPageNumber = pageSize * (pageNumber + 1) >= list.size() ? null : pageNumber + 1;
this.pageElements = list.subList(this.firstElementIndex, this.lastElementIndex);
}
/**
* Returns the elements of this page as a list.
*
* @return The elements of this page
*/
public List<E> getPageElements() {
return this.pageElements;
}
/**
* Decides whether there is a previous page for this one.
*
* @return true if this page has a previous one, false otherwise
*/
public boolean hasPreviousPage() {
return this.previousPageNumber != null;
}
/**
* Returns the number of the previous page (0-indexed).
*
* @return The number of the previous page
* @throws NoSuchElementException if there is no previous page
*/
public int getPreviousPageNumber() throws NoSuchElementException {
if (this.previousPageNumber == null) {
throw new NoSuchElementException("previous page");
}
return this.previousPageNumber;
}
/**
* Decides whether there is a next page for this one.
*
* @return true if this page has a next one, false otherwise
*/
public boolean hasNextPage() {
return this.nextPageNumber != null;
}
/**
* Returns the number of the next page (0-indexed).
*
* @return The number of the next page
* @throws NoSuchElementException if there is no next page
*/
public int getNextPageNumber() throws NoSuchElementException {
if (this.nextPageNumber == null) {
throw new NoSuchElementException("next page");
}
return this.nextPageNumber;
}
/**
* Returns the index of the first element of this page in the whole list
* (0-indexed, inclusive).
*
* @return The index of the first element of this page
*/
public int getFirstElementIndex() {
return this.firstElementIndex;
}
/**
* Returns the index of the last element of this page in the whole list
* (0-indexed, exclusive).
*
* @return The index of the last element of this page
*/
public int getLastElementIndex() {
return this.lastElementIndex;
}
}
package fr.greyc.mad.datastructures;
import java.util.HashSet;
import java.util.Set;
/**
* A class for representing a selection of elements from a set in a sparse
* manner, through a default status (selected/deselected) plus a set of
* exceptions, that is, of elements with the opposite status.
* <p>
* An important point is that the instances of this class are not aware of the
* whole set of elements, so that it only makes sense to hold this set fixed,
* and to explicitly change the status only of its elements, for both the
* selected and unselected sets of elements to be unambiguously defined.
* <p>
* Another important point is that sparsity is not guaranteed (as would be if
* the default status were always the majoritary one). The representation
* instead bets that the selection will be built with that majority status as
* the default one, and that this status will be explicitly changed using
* {@link #selectAll(boolean)} whenever the majority changes.
*
* @author zanuttini
*
* @param <E> The type of elements
*/
public class Selection<E> {
/**
* The status for all elements but those stored as exceptions (true for
* selected, false for unselected).
*/
private boolean defaultStatus;
/** The set of elements whose status is the opposite of the default one. */
private final Set<E> exceptions;
/**
* Builds a new selection with a given status for all elements.
*
* @param selected Whether all elements are selected (true) or all are
* unselected (false)
*/
public Selection(boolean selected) {
this.defaultStatus = selected;
this.exceptions = new HashSet<>();
}
/**
* Selects or unselects all elements.
*
* @param select true for selecting all elements, false for unselecting all
* elements
*/
public void selectAll(boolean select) {
this.defaultStatus = select;
this.exceptions.clear();
}
/**
* Decides whether an element is selected. The behavior is not defined if the
* element is not one of the implicit global set of elements.
*
* @param element An element
* @return true if the given element is selected according to this instance,
* false if it is unselcted
*/
public boolean isSelected(E element) {
// Either sequences are selected by default and this one is no exception,
// or sequences are unselected by default and this one is an exception.
return this.defaultStatus != this.exceptions.contains(element);
}
/**
* Selects or unselects an element.
*
* @param element An element
* @param select true for selecting the element, false for unselecting it
*/
public void select(E element, boolean select) {
if (this.defaultStatus != select) {
this.exceptions.add(element);
} else {
this.exceptions.remove(element);
}
}
/**
* Returns the number of elements which are selected, given the total number of
* sequences in the implicit global set (and tacitly assuming that all elements
* with an explicit selection status belong to this set).
*
* @param nbElements A number of elements
* @return The number of selected elements given that there are nbElements in
* the implicit global set
* @throws IllegalArgumentException if more than the given number of elements
* have an explicit status in this selection
*/
public int nbSelected(int nbElements) throws IllegalArgumentException {
if (nbElements < this.exceptions.size()) {
throw new IllegalArgumentException("More elements than the given number have been explicitly set: "
+ this.exceptions.size() + " vs " + nbElements);
}
return this.defaultStatus ? nbElements - this.exceptions.size() : this.exceptions.size();
}
}
package fr.greyc.mad.datastructures;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
/**
* A class for representing a union of intervals of integers. An example is
* [-10,0] union [3,3] union [5,9]. The representation is always kept minimal in
* the sense that, say, ([0,1] union [3,4] union [4,9]) or ([0,1] union [3,5]
* union [4,9]) would both be represented as ([0,1] union [3,9]).
*
* @author Bruno Zanuttini, Universit&eacute; de Caen Basse-Normandie, France
*
* TODO: equals, hashCode
*/
public class UnionOfIntervals {
// Implementation note: the intervals are stored as semi-open intervals (to the
// right), that is, [i,j] is stored as [i,j+1[
/**
* An ordered list of integers representing intervals of the form
* (start_1,end_1,start_2,end_2,...) where the i-th interval is [start_i,end_i[.
*/
protected List<Integer> intervals;
/**
* Builds a new instance with an empty set of intervals.
*/
public UnionOfIntervals() {
this.intervals = new ArrayList<Integer>();
}
/**
* Decides whether a given integer is the first in some interval.
*
* @param i An integer
* @return true if there is an interval [i,.] in this union of intervals, false
* otherwise
*/
public boolean startsInterval(int i) {
boolean isLowerBound = true; // true iff the integer currently considered in this.intervals is a lower bound
for (int bound : this.intervals) {
if (bound == i && isLowerBound)
return true;
if (bound > i)
return false;
isLowerBound = !isLowerBound;
}
return false;
}
/**
* Decides whether a given integer is the last in some interval.
*
* @param i An integer
* @return true if there is an interval [.,i] in this union of intervals, false
* otherwise
*/
public boolean endsInterval(int i) {
boolean isUpperBound = false; // true iff the position currently considered in this.intervals is an upper
// bound
for (int bound : this.intervals) {
if (bound == i + 1 && isUpperBound)
return true;
if (bound > i + 1)
return false;
isUpperBound = !isUpperBound;
}
return false;
}
/**
* Decides whether a given integer is in some interval.
*
* @param i An integer
* @return true if there is an interval [a,b] in this union of intervals which
* contains i, false otherwise
*/
public boolean contains(int i) {
ListIterator<Integer> iterator = this.intervals.listIterator();
int currentLowerBound, currentUpperBound;
while (iterator.hasNext()) {
currentLowerBound = iterator.next();
currentUpperBound = iterator.next();
if (currentLowerBound <= i && i <= currentUpperBound)
return true;
}
return false;
}
/**
* Restricts this union of intervals to given bounds. For instance, restricting
* ([-10,-8] union [-4,1] union [3,9]) to 0,10 results in the union of intervals
* ([0,1] union [3,9])
*
* @param lowerBound The first integer to be kept
* @param upperBound The last index to be kept
*/
public void restrict(int lowerBound, int upperBound) {
int currentLowerBound, currentUpperBound;
ListIterator<Integer> iterator = this.intervals.listIterator();
while (iterator.hasNext()) {
currentLowerBound = iterator.next();
currentUpperBound = iterator.next();
if (currentUpperBound <= lowerBound) {
iterator.remove();
iterator.previous();
iterator.remove();
} else if (currentLowerBound < lowerBound) {
iterator.previous();
iterator.previous();
iterator.set(lowerBound);
iterator.next();
iterator.next();
} else if (currentLowerBound > upperBound) {
iterator.remove();
iterator.previous();
iterator.remove();
} else if (currentUpperBound > upperBound + 1) {
iterator.set(upperBound + 1);
}
}
}
/**
* Adds a given union of intervals to this one.
*
* @param unionOfIntervals A union of intervals
*/
public void addAll(UnionOfIntervals unionOfIntervals) {
Iterator<Integer> iterator = unionOfIntervals.intervals.iterator();
while (iterator.hasNext())
this.addInterval(iterator.next(), iterator.next());
}
/**
* Adds the same integer to all elements in this union of intervals. For
* instance, adding 5 to ([-10,-8] union [-4,1] union [3,9]) results in the
* union of intervals ([-5,-3] union [1,6] union [8,14]).
*
* @param value The value to add (may be negative)
*/
public void addOffset(int value) {
ListIterator<Integer> iterator = this.intervals.listIterator();
int current;
while (iterator.hasNext()) {
current = iterator.next();
iterator.previous();
iterator.set(current + value);
iterator.next();
}
}
/**
* Adds an integer to this union of intervals (the integer i is seen as the
* interval [i,i]).
*
* @param i The integer to add
*/
public void add(int i) {
this.addInterval(i, i);
}
/**
* Adds an interval of integers to this union of intervals.
*
* @param lowerBound The first element of the interval
* @param upperBound The last element of the interval (the interval is
* [lowerBound,upperBound])
* @throws IllegalArgumentException if the interval is not well-formed, i.e.,
* upperBound is not geq lowerBound
*/
public void addInterval(int lowerBound, int upperBound) throws IllegalArgumentException {
if (upperBound < lowerBound)
throw new IllegalArgumentException(
"The interval [" + lowerBound + "," + upperBound + "] is not well-formed");
ListIterator<Integer> iterator = this.intervals.listIterator();
boolean isLowerBound = true; // true iff the position currently considered in this.intervals is a lower bound
boolean inserted = false;
int current;
// Inserting lower bound
while (iterator.hasNext()) {
current = iterator.next();
if (isLowerBound && lowerBound < current) {
iterator.previous();
iterator.add(lowerBound);
inserted = true;
break;
}
if (isLowerBound && lowerBound >= current) {
if (lowerBound < iterator.next()) {
isLowerBound = !isLowerBound;
inserted = true;
iterator.previous();
break;
}
isLowerBound = !isLowerBound;
iterator.previous();
continue;
}
if (!isLowerBound && lowerBound <= current) {
iterator.previous();
inserted = true;
break;
}
isLowerBound = !isLowerBound;
}
if (!inserted) {
iterator.add(lowerBound);
iterator.add(upperBound + 1);
return;
}
// Inserting upper bound
while (iterator.hasNext()) {
current = iterator.next();
if (isLowerBound && upperBound + 1 < current) {
iterator.previous();
iterator.add(upperBound + 1);
return;
}
if (isLowerBound && upperBound + 1 == current) {
iterator.previous();
iterator.remove();
return;
}
if (isLowerBound && upperBound >= current) {
iterator.previous();
iterator.remove();
}
if (!isLowerBound && upperBound < current) {
return;
}
if (!isLowerBound && upperBound >= current) {
iterator.previous();
iterator.remove();
}
isLowerBound = !isLowerBound;
}
iterator.add(upperBound + 1);
}
@Override
public String toString() {
String res = "";
boolean isLowerBound = true; // true iff the position currently considered in this.intervals is a lower bound
for (int bound : this.intervals) {
if (isLowerBound)
res += "[" + bound + ",";
else
res += (bound - 1) + "] ";
isLowerBound = !isLowerBound;
}
return res;
}
}
/**
* Classes for various data structures.
*
* @author Bruno Zanuttini, Université de Caen Normandie, France
*/
package fr.greyc.mad.datastructures;
\ No newline at end of file
package fr.greyc.mad.observable;
import java.util.ArrayList;
import java.util.List;
/**
* A basic implementation of interface {@link Observed}.
*
* @author Bruno Zanuttini, Université de Caen Normandie, France
*
* @param <N> The type of notifications
*
* TODO: change to PropertyChangeSupport?
*/
public class BasicObserved<N> implements Observed<N> {
/** The list of observers of this instance. */
private final List<Observer<N>> observers;
/**
* Builds a new instance with no observer.
*/
public BasicObserved() {
this.observers = new ArrayList<>();
}
@Override
public List<Observer<N>> getObservers() {
return this.observers;
}
@Override
public void addObserver(Observer<N> observer) {
if (!this.observers.contains(observer)) {
this.observers.add(observer);
}
}
@Override
public void removeObserver(Observer<N> observer) {
this.observers.remove(observer);
}
@Override
public void notify(N notification) {
for (Observer<N> observer : this.observers) {
observer.onNotification(this, notification);
}
}
}
package fr.greyc.mad.observable;
/**
* An abstract class for representing ongoing processes which can be monitored,
* and possibly cancelled. As a general rule, an instance of a Monitorable
* executor is supposed to represent one execution, and hence should not be
* "executed" several times.
*
* @author Bruno Zanuttini, Université de Caen Normandie, France
*/
public abstract class MonitorableExecutor extends BasicObserved<Progress> {
/** A human-readable description for this instance. */
protected final String description;
/**
* Builds a new instance.
*
* @param description A human-readable description for the instance
*/
public MonitorableExecutor(String description) {
this.description = description;
}
/**
* Returns the human-readable description for this instance.
*
* @return The human-readable description for this instance
*/
public String getDescription() {
return this.description;
}
/**
* Cancels the execution of this process.
*/
public abstract void cancel();
}
package fr.greyc.mad.observable;
import java.util.List;
/**
* An interface for objects which can be observed, through notifications sent to
* all observers.
*
* <p>
* As a general rule, the order of observers matters; they are notified in
* order. Each notification is blocking (except of course if the observer runs
* asynchronously).
*
* @see Observer
*
* @author Bruno Zanuttini, Université de Caen Normandie, France
*
* @param <N> The type of notifications
*
* TODO: change to PropertyChangeSupport?
*/
public interface Observed<N> {
/**
* Returns the list of observers of this instance.
* @return The list of observers of this instance
*/
List<Observer<N>> getObservers();
/**
* Adds an observer at the end of the current list. Nothing occurs if the
* observer is already registered.
*
* @param observer An observer
*/
void addObserver(Observer<N> observer);
/**
* Removes an observer from the current list. Nothing occurs if the observer was
* not registered.
*
* @param observer An observer
*/
void removeObserver(Observer<N> observer);
/**
* Notifies all observers.
*
* @param notification The notification to send
*/
void notify(N notification);
}
\ No newline at end of file
package fr.greyc.mad.observable;
/**
* An interface for observers of notifications.
*
* @see Observed
*
* @author Bruno Zanuttini, Université de Caen Normandie, France
*
* @param <N> The type of notifications received
*
* TODO: change to PropertyChangeListener?
*/
public interface Observer<N> {
/**
* Called when this observer is notified.
*
* @param notifier The notifier
* @param notification The notification
*/
public void onNotification(BasicObserved<N> notifier, N notification);
}
package fr.greyc.mad.observable;
/**
* A class holding information about the progress of a process. The intended use
* is as a type for notifications by {@link Observed} objects.
* <p>
* Each instance holds a numeric value. The semantics of this value is not
* specified, so that it may be used differently in different contexts. Typical
* uses would be for specifying how many atomic operations were performed, or
* how many remain to do, for instance.
*
* @author Bruno Zanuttini, Université de Caen Normandie, France
*/
public class Progress {
/** An enumeration of possible types of progress. */
public enum ProgressType {
/** Indicates that the process has started. */
STARTED,
/** Indicates that the process has progressed. */
PROGRESSED,
/** Indicates that the process ended normally. */
FINISHED,
/** Indicates that the process was cancelled. */
CANCELLED,
/** Indicates that there was an error during the execution of the process. */
ERROR;
}
/** The executor whose progress is concerned. */
protected final MonitorableExecutor executor;
/** The type of this instance. */
protected final Progress.ProgressType type;
/** The value associated to this instance. */
protected final long value;
/**
* The error message for this instance (nonnull if and only if the type if
* {@code ERROR}.
*/
protected final String errorMessage;
/**
* Builds a new instance with type {@code ERROR}.
*
* @param executor The executor whose progress is monitored
* @param value The value associated to this instance
* @param errorMessage The error message for this instance
*/
public Progress(MonitorableExecutor executor, long value, String errorMessage) {
super();
this.executor = executor;
this.type = ProgressType.ERROR;
this.value = value;
this.errorMessage = errorMessage;
}
/**
* Builds a new instance with any type but {@code ERROR}.
*
* @param executor The executor whose progress is monitored
* @param type The type of this instance
* @param value The value associated to this instance
* @throws IllegalArgumentException if the type is {@code ERROR}
*/
public Progress(MonitorableExecutor executor, Progress.ProgressType type, long value)
throws IllegalArgumentException {
super();
if (type == ProgressType.ERROR) {
throw new IllegalArgumentException("Error notifications must come with an error message");
}
this.executor = executor;
this.type = type;
this.value = value;
this.errorMessage = null;
}
/**
* Returns the executor whose progress is concerned.
*
* @return The executor whose progress is concerned
*/
public MonitorableExecutor getExecutor() {
return this.executor;
}
/**
* Returns the type of this instance.
*
* @return The type of this instance
*/
public Progress.ProgressType getType() {
return type;
}
/**
* Returns the value associated to this instance.
*
* @return The value associated to this instance
*/
public long getValue() {
return value;
}
/**
* Returns the error message for this instance.
*
* @return The error message for this instance
* @throws IllegalStateException if the type of this instance is not
* {@code ERROR}
*/
public String getErrorMessage() throws IllegalStateException {
if (!this.type.equals(ProgressType.ERROR)) {
throw new IllegalStateException("Only error notifications have a message");
}
return this.errorMessage;
}
}
\ No newline at end of file
/**
* Classes for observable processes (and classes in general). Specifically, this
* package defines classes for monitorable processes (i.e., whose progress can
* be monitored).
*
* @author Bruno Zanuttini, Université de Caen Normandie, France
*/
package fr.greyc.mad.observable;
\ No newline at end of file
package fr.greyc.mad.resources;
import java.util.HashSet;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.function.Consumer;
/**
* A base class for handlers of a resource bundle, whose main purpose is to
* provide facilities for performing operations only for those resources which
* are defined in the bundle, to check whether all defined resources have been
* used, etc.
* <p>
* In general, a key is said to be "used" if it is a key in the bundle or in one
* of its parents, and the associated object has been retrieved, even if this
* object did not have the expected type or its subsequent treatment ended
* abnormally. A key is said to be "nonexisting" if it has been attempted to
* retrieve the associated object, but the key is not one in the bundle or in
* one of its parents.
* <p>
* Instances of this class are not instances of ResourceBundle. Should a method
* be called on the wrapped bundle, the method {@link #getBundle()} can be used.
*
* @author Bruno Zanuttini, Université de Caen Normandie, France
*/
public class ResourceHandler {
/** The resource bundle. */
protected final ResourceBundle bundle;
/** The set of all used keys. */
private final Set<String> used;
/** The set of all keys which have been checked but do not exist. */
private final Set<String> nonexisting;
/**
* Builds a new instance.
*
* @param bundle A resource bundle
*/
public ResourceHandler(ResourceBundle bundle) {
this.bundle = bundle;
this.used = new HashSet<>();
this.nonexisting = new HashSet<>();
}
/**
* Returns the resource bundle.
*
* @return The resource bundle
*/
public ResourceBundle getBundle() {
return this.bundle;
}
/**
* Performs an operation with the string associated to a given key, throwing an
* exception if the key does not exist in the bundle nor in any of its parent.
* It the key exists, additionally marks it as used, even if it is not
* associated with a string, or handling it throws an exception or otherwise
* ends abnormally. If the key does not exist, marks it as nonexisting.
*
* @param handler The operation to perform, called with the string associated to
* the key
* @param key A key in the bundle
* @throws MissingResourceException if the key does not exist in the bundle (nor
* in any of its parents)
* @throws ClassCastException if the key exists but the associated object
* is not a string
*/
public void handleString(Consumer<String> handler, String key) throws MissingResourceException, ClassCastException {
if (!this.bundle.containsKey(key)) {
this.setUsed(key);
} else {
this.setNonexisting(key);
}
String str = this.bundle.getString(key);
handler.accept(str);
}
/**
* Performs an operation with the string associated to a given key, only if the
* key exists in the bundle or in one of its parent (otherwise does nothing). If
* the key exists and is indeed associated with a string, additionally marks it
* as used, even if handling it throws an exception or otherwise ends
* abnormally. If the key does not exist, marks it as nonexisting.
*
* @param handler The operation to perform, called with the string associated to
* the key
* @param key A key in the bundle
* @throws ClassCastException if the key exists but the associated object is not
* a string
*/
public void handleStringIfExists(Consumer<String> handler, String key) throws ClassCastException {
if (this.bundle.containsKey(key)) {
this.setUsed(key);
handler.accept(this.bundle.getString(key));
} else {
this.setNonexisting(key);
}
}
/**
* Performs an operation with the string array associated to a given key,
* throwing an exception if the key does not exist in the bundle nor in any of
* its parent. It the key exists, additionally marks it as used, even if it is
* not associated with a string array, or handling it throws an exception or
* otherwise ends abnormally. If the key does not exist, marks it as
* nonexisting.
*
* @param handler The operation to perform, called with the string array
* associated to the key
* @param key A key in the bundle
* @throws MissingResourceException if the key does not exist in the bundle (nor
* in any of its parents)
* @throws ClassCastException if the key exists but the associated object
* is not a string array
*/
public void handleStringArray(Consumer<String[]> handler, String key)
throws MissingResourceException, ClassCastException {
if (this.bundle.containsKey(key)) {
this.setUsed(key);
} else {
this.setNonexisting(key);
}
String[] str = this.bundle.getStringArray(key);
handler.accept(str);
}
/**
* Performs an operation with the string array associated to a given key, only
* if the key exists in the bundle or in one of its parent (otherwise does
* nothing). If the key exists and is indeed associated with a string array,
* additionally marks it as used, even if handling it throws an exception or
* otherwise ends abnormally. If the key does not exist, marks it as
* nonexisting.
*
* @param handler The operation to perform, called with the string array
* associated to the key
* @param key A key in the bundle
* @throws ClassCastException if the key exists but the associated object is not
* a string array
*/
public void handleStringArrayIfExists(Consumer<String[]> handler, String key) throws ClassCastException {
if (this.bundle.containsKey(key)) {
this.setUsed(key);
handler.accept(this.bundle.getStringArray(key));
} else {
this.setNonexisting(key);
}
}
/**
* Performs an operation with the object associated to a given key, throwing an
* exception if the key does not exist in the bundle nor in any of its parent.
* It the key exists, additionally marks it as used, even if handling it throws
* an exception or otherwise ends abnormally. If the key does not exist, marks
* it as nonexisting.
*
* @param handler The operation to perform, called with the string associated to
* the key
* @param key A key in the bundle
* @throws MissingResourceException if the key does not exist in the bundle (nor
* in any of its parents)
*/
public void handle(Consumer<Object> handler, String key) throws MissingResourceException {
if (this.bundle.containsKey(key)) {
this.setUsed(key);
} else {
this.setNonexisting(key);
}
Object str = this.bundle.getObject(key);
handler.accept(str);
}
/**
* Performs an operation with the object associated to a given key, only if the
* key exists in the bundle or in one of its parent (otherwise does nothing). If
* the key exists, additionally marks it as used, even if handling it throws an
* exception or otherwise ends abnormally. If the key does not exist, marks it
* as nonexisting.
*
* @param handler The operation to perform, called with the string associated to
* the key
* @param key A key in the bundle
*/
public void handleIfExists(Consumer<Object> handler, String key) {
if (this.bundle.containsKey(key)) {
this.setUsed(key);
handler.accept(this.bundle.getObject(key));
} else {
this.setNonexisting(key);
}
}
/**
* Decides whether a key has been marked as used. Note that if the key does not
* exist in the bundle (nor in any of its parents), then by construction this
* call will return false.
*
* @param key A key
* @return true if the key has been marked as used, false otherwise
*/
public boolean used(String key) {
return this.used.contains(key);
}
/**
* Decides whether all the keys of the bundle have been marked as used.
*
* @return true if all the keys of the bundle have been marked as used, false
* otherwise
*/
public boolean allUsed() {
return this.used.equals(bundle.keySet());
}
/**
* Returns the set of all keys of the bundle (or one of its parents) which have
* not been marked as used.
*
* @return The set of all keys which have not been marked as used
*/
public Set<String> unused() {
Set<String> res = new HashSet<>(bundle.keySet());
res.removeAll(this.used);
return res;
}
/**
* Returns the set of all keys which have been marked as nonexisting.
*
* @return The set of all keys which have been marked as nonexisting
*/
public Set<String> nonexisting() {
return this.nonexisting;
}
/**
* Marks a key as used.
*
* @param key A key
* @throws IllegalArgumentException if the key is not one of the bundle (nor of
* any of its parents)
*/
protected void setUsed(String key) throws IllegalArgumentException {
if (!this.bundle.containsKey(key)) {
throw new IllegalArgumentException("Key does not exist: " + key);
}
this.used.add(key);
}
/**
* Marks a key as nonexisting.
*
* @param key A key
* @throws IllegalArgumentException if the key is one of the bundle (or of one
* of its parents)
*/
protected void setNonexisting(String key) throws IllegalArgumentException {
if (this.bundle.containsKey(key)) {
throw new IllegalArgumentException("Key exists: " + key);
}
this.nonexisting.add(key);
}
}
package fr.greyc.mad.resources;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.ResourceBundle.Control;
/**
* A control for resource bundles forcing reading in UTF-8, taken from <a href=
* "https://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle">https://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle</a>.
*/
public class UTF8Control extends Control {
/**
* Builds a new instance.
*/
public UTF8Control() {
super();
}
@Override
public List<String> getFormats(String baseName) {
return FORMAT_PROPERTIES;
}
@Override
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException {
if (!format.equals("java.properties")) {
return super.newBundle(baseName, locale, format, loader, reload);
}
// The below is a copy of the default implementation.
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, "properties");
ResourceBundle bundle = null;
InputStream stream = null;
if (reload) {
URL url = loader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
connection.setUseCaches(false);
stream = connection.getInputStream();
}
}
} else {
stream = loader.getResourceAsStream(resourceName);
}
if (stream != null) {
try {
// Only this line is changed to make it to read properties files as UTF-8.
bundle = new PropertyResourceBundle(new InputStreamReader(stream, StandardCharsets.UTF_8));
} finally {
stream.close();
}
}
return bundle;
}
}
/**
* Facilities for handling resources.
*
* @author Bruno Zanuttini, Université de Caen Normandie, France
*/
package fr.greyc.mad.resources;
\ No newline at end of file
package fr.greyc.mad.userinput;
import java.util.ArrayList;
import java.util.List;
/**
* An abstract class for objects able to build other objects from fields.
*
* A builder has an internal state, which consists of the state of the fields as
* well as a global assessment of validity of the parameters, to be used when
* there are interfield constraints. How this is assessed is to be defined by
* subclasses.
*
* The order of fields is relevant: they are retrieved in the same order as they
* were added.
*
* @author Bruno Zanuttini, Université de Caen Normandie, France
*
* @param <F> The type for fields
* @param <T> The type of the objects built
*/
public abstract class AbstractBuilder<F extends Field<?>, T> {
/** The current list of fields in this builder. */
protected List<F> fields;
/** The help string for this builder. */
protected final String helpString;
/**
* Builds a new instance.
*
* @param fields The initial list of fields in this instance
* @param helpString The help string for this builder
*/
public AbstractBuilder(List<F> fields, String helpString) {
this.fields = fields;
this.helpString = helpString;
}
/**
* Builds a new instance with an empty initial list of fields.
*
* @param helpString The help string for this builder
*/
public AbstractBuilder(String helpString) {
this(new ArrayList<>(), helpString);
}
/**
* Adds a field to this instance.
*
* The new field is added after the previous ones.
*
* @param field A field
* @throws IllegalArgumentException if the field is already contained in this
* instance
*/
public void addField(F field) throws IllegalArgumentException {
if (this.fields.contains(field)) {
throw new IllegalArgumentException("Field " + field + " is already contained in builder");
}
this.fields.add(field);
}
/**
* Returns the fields in this instance.
*
* @return The fields in this instance
*/
public List<F> getFields() {
return this.fields;
}
/**
* Decides whether the parameters currently input in the fields are valid
* (individually and together).
*
* @return true if the parameters currently input are valid, false otherwise
*/
public boolean isValid() {
for (F field : this.fields) {
if (!field.isValid()) {
return false;
}
}
return this.isGloballyValid();
}
/**
* Returns the object defined by the parameters currently input in the fields,
* if they are valid (individually and together).
*
* @return The object defined by the parameters currently input in the fields
* @throws IllegalStateException If the current parameters are not valid (either
* some of them or together)
*/
public T getObject() throws IllegalStateException {
if (!this.isValid()) {
throw new IllegalStateException("Cannot build an object: data is not valid");
}
return this.safeGetObject();
}
/**
* Returns the help string for this builder.
*
* @return The help string for this builder
*/
public String getHelpString() {
return this.helpString;
}
/**
* Returns the help string for a field of this builder. The implementation
* forwards the call to the field, but this behavior may be overridden by
* subclasses in case the help string for a field depends on the builder or the
* builder state.
*
* @param field A field of this builder
* @return The help string for the given field
* @throws IllegalArgumentException if the field is not one of this builder's
*/
public String getHelpString(InputField<String, ?> field) throws IllegalArgumentException {
if (!this.fields.contains(field)) {
throw new IllegalArgumentException("Not a field of this builder: " + field);
}
return field.getHelpString();
}
/**
* Decides whether the parameters currently input are consistent together.
*
* This method may be called even when the parameters are not individually
* valid. That this method returns true does not mean that the object is ready
* to be built. This is guaranteed only if this method returns true AND all
* fields are currently individually valid.
*
* @return true if the parameters currently input are consistent together, false
* otherwise
*/
public abstract boolean isGloballyValid();
/**
* Returns the errors pertaining to the global consistency of the parameters
* currently input. The list is empty if these parameters are currently
* consistent together, and otherwise there is a single-line string for each
* error.
*
* @return The errors pertaining to the global consistency of the parameters
* currently input
*/
public abstract List<String> getGlobalErrors();
/**
* Returns the object defined by the parameters currently input, assuming
* (without checking) that they are valid (both individually and together).
*
* @return The object defined by the parameters currently input
*/
protected abstract T safeGetObject();
}
package fr.greyc.mad.userinput;
import java.util.Arrays;
import java.util.List;
/**
* A bound value input field for Boolean values from strings. Note that
* {@code null} is NOT a valid value for this field (it encodes the absence of
* input and value, like with general input fields.
*
* @author Bruno Zanuttini, Université de Caen Normandie, France
*/
public class BooleanInputFieldFromString extends InputFieldFromStringAdapter<Boolean> {
/**
* Boolean values {@code true} and {@code false}, wrapped in a list. The order
* in this list defines the order of values in the sense of
* {@link BoundValueField}.
*/
public static List<Boolean> booleans = Arrays.asList(true, false);
/** The string encoding {@code true}. */
protected final String trueString;
/** The string encoding {@code false}. */
protected final String falseString;
/**
* Builds a new instance.
*
* @param isOptional Whether this field is optional (true) or not (false)
* @param label The label for this field
* @param trueString The string encoding {@code true}
* @param falseString The string encoding {@code false}
* @param helpString The help string for this field
*/
public BooleanInputFieldFromString(boolean isOptional, String label, String trueString, String falseString,
String helpString) {
super(new BoundValueField<>(isOptional, label, BooleanInputFieldFromString.booleans, helpString));
this.trueString = trueString;
this.falseString = falseString;
}
/**
* Builds a new instance with default encodings for {@code true} and
* {@code false}, namely {@code Boolean.TRUE.toString()} and
* {@code Boolean.FALSE.toString()}, respectively.
*
* @param isOptional Whether this field is optional (true) or not (false)
* @param label The label for this field
* @param helpString The help string for this field
*/
public BooleanInputFieldFromString(boolean isOptional, String label, String helpString) {
this(isOptional, label, Boolean.TRUE.toString(), Boolean.FALSE.toString(), helpString);
}
/**
* Builds a new instance with an initial input.
*
* @param isOptional Whether this field is optional (true) or not (false)
* @param label The label for this field
* @param trueString The string encoding {@code true}
* @param falseString The string encoding {@code false}
* @param initialInput The initial input for this field (not necessarily one
* encoding a valid value)
* @param helpString The help string for this field
*/
public BooleanInputFieldFromString(boolean isOptional, String label, String trueString, String falseString,
String initialInput, String helpString) {
this(isOptional, label, trueString, falseString, helpString);
this.setInput(initialInput);
}
@Override
public String getInputFor(Boolean value) throws IllegalArgumentException {
return value ? this.trueString : this.falseString;
}
@Override
protected Boolean tryGetValue(String input) throws IllegalArgumentException {
if (this.trueString.equals(input)) {
return Boolean.TRUE;
} else if (this.falseString.equals(input)) {
return Boolean.FALSE;
} else {
throw new IllegalArgumentException("No Boolean value corresponds to string \"" + input + "\"");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment